gpt4 book ai didi

python速成类(class)使未注册用户可以访问django帖子

转载 作者:行者123 更新时间:2023-12-04 08:17:18 25 4
gpt4 key购买 nike

我目前正在尝试做这个 python 速成类(class)的练习,但不确定如何进行。这是有问题的练习。

Then try adding a more advanced feature,
such as giving users the option of making a topic public. This would require an
attribute called public as part of the Topic model (this should be set to False
by default) and a form element on the new_topic page that allows the user to
change a topic from private to public.
以下是我到目前为止所做的更改
在forms.py中,我的理解是当用户在“公共(public)”文本中键入“真”或“假”时,主题模型的公共(public)属性会相应更新吗?
from django import forms

from .models import Topic, Entry

class TopicForm(forms.ModelForm):
class Meta:
model = Topic
fields = ['text', 'public']
labels = {'text': '', 'public' : "True/False"}

class EntryForm(forms.ModelForm):
class Meta:
model = Entry
fields = ['text']
labels = {'text': 'Entry:'}
widgets = {'text': forms.Textarea(attrs={'cols': 80})}
models.py 中的主题模型
from django.db import models
from django.contrib.auth.models import User

# Create your models here.

class Topic(models.Model):
"""A topic the user is learning about."""
text = models.CharField(max_length=200)
date_added = models.DateTimeField(auto_now_add=True)
owner = models.ForeignKey(User, on_delete=models.CASCADE)
public = models.BooleanField(default=False)

def __str__(self):
"""Return a string representation of the model."""
return self.text
我目前的猜测是,在主题函数下的views.py中,我必须对其进行编辑,以便测试所有者ID或查看者是否尚未登录,然后相应地返回所有公共(public)主题。因此,我恳请您在正确的方向左右寻求刺激。谢谢大家!
views.py 中的当前主题功能
@login_required
def topics(request):
"""Show all topics."""
topics = Topic.objects.filter(owner=request.user).order_by('date_added')
context = {'topics': topics}
return render(request, 'learning_logs/topics.html', context)
得到帮助后的最终代码:
表格.py
class TopicForm(forms.ModelForm):
class Meta:
model = Topic
fields = ['text', 'public']
labels = {'text': '', 'public' : "Tick to set to public"}
模型.py
class Topic(models.Model):
"""A topic the user is learning about."""
text = models.CharField(max_length=200)
date_added = models.DateTimeField(auto_now_add=True)
owner = models.ForeignKey(User, on_delete=models.CASCADE)
public = models.BooleanField(default=False)

def __str__(self):
"""Return a string representation of the model."""
return self.text
View .py
def topics(request):
"""Show all topics."""
if request.user.is_authenticated:
topics = Topic.objects.filter(owner=request.user).order_by('date_added')
else:
topics = Topic.objects.filter(public=True).order_by('date_added')
context = {'topics': topics}
return render(request, 'learning_logs/topics.html', context)

最佳答案

您可以检查用户是否已登录,并根据此过滤器仅针对公共(public)项目或公共(public)项目与用户是所有者的项目组合:

from django.db.models import Q

def topics(request):
if request.user.is_authenticated:
topics = Topic.objects.filter(…)
else:
topics = Topic.objects.filter(…)
topics = topics.order_by('date_added')
context = {'topics': topics}
return render(request, 'learning_logs/topics.html', context)
还需要填写的部分。用户认证的部分需要 Q objects [Django-doc]实现逻辑析取。

关于python速成类(class)使未注册用户可以访问django帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65654149/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com