Python (隐藏字段作者)此字段为必填字段
我遵循了django教程,学习如何在博客帖子中限制其他用户。Python (隐藏字段作者)此字段为必填字段,python,django,blogs,Python,Django,Blogs,我遵循了django教程,学习如何在博客帖子中限制其他用户。 (视频链接:) 但是当我试着发帖的时候,我得到了这个信息 (隐藏字段作者)此字段是必填字段吗 这是my views.py文件 from .forms import PostForm, EditForm class AddPostView(CreateView, LoginRequiredMixin): model = Post form_class = PostForm template_name = &qu
(视频链接:) 但是当我试着发帖的时候,我得到了这个信息 (隐藏字段作者)此字段是必填字段吗 这是my views.py文件
from .forms import PostForm, EditForm
class AddPostView(CreateView, LoginRequiredMixin):
model = Post
form_class = PostForm
template_name = "add_post.html"
# fields = "__all__"
这是我的forms.py文件
from .models import Post, Category
from django import forms
# choices = [('uncategorized', "uncategorized"), ("gaming", "gaming"), ("youtube", "youtube"),]
choices = Category.objects.all().values_list('name', 'name')
choice_list = []
for item in choices:
choice_list.append(item)
class PostForm(forms.ModelForm):
class Meta:
model = Post
fields = ("title", "title_tag", "author", "category", "body")
widgets = {
"title": forms.TextInput(attrs={"class": "form-control", "placeholder": "Title"}),
"title_tag": forms.TextInput(attrs={"class": "form-control", "placeholder": "Title Tag"}),
"author": forms.TextInput(attrs={"class": "form-control", "id": "Rashaad", "value": "", "type": "hidden"}),
# "author": forms.Select(attrs={"class": "form-control"}),
"category": forms.Select(choices=choice_list, attrs={"class": "form-control"}),
"body": forms.Textarea(attrs={"class": "form-control", "placeholder": "Body"}),
}
class EditForm(forms.ModelForm):
class Meta:
model = Post
fields = ("title", "title_tag", "category", "body")
widgets = {
"title": forms.TextInput(attrs={"class": "form-control", "placeholder": "Title"}),
"title_tag": forms.TextInput(attrs={"class": "form-control", "placeholder": "Title Tag"}),
# "author": forms.Select(attrs={"class": "form-control"}),
"category": forms.Select(choices=choice_list, attrs={"class": "form-control"}),
"body": forms.Textarea(attrs={"class": "form-control", "placeholder": "Body"}),
}
我的add_post.html文件
{% extends 'base.html' %}
{% block content %}
{% if user.is_authenticated %}
<head>
<title>Add Post...</title>
</head>
<h1>Add Blog Post...</h1>
<br/><br/>
<div class="form-group">
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<button class="btn btn-secondary">Post</button>
</form>
</div>
<script>
var name = "{{ user.id }}";
document.getElementsByName("Rashaad").value;
</script>
{% else %}
<title>You are not allowed here</title>
<h1>You are not allowed here</h1>
<h2>(And you Know it)</h2>
{% endif %}
{% endblock %}
{%extends'base.html%}
{%block content%}
{%if user.u经过身份验证%}
添加帖子。。。
添加博客帖子。。。
{%csrf_令牌%}
{{form.as_p}}
邮递
var name=“{user.id}}”;
document.getElementsByName(“Rashaad”).value;
{%else%}
你不允许在这里
你不允许在这里
(你知道的)
{%endif%}
{%endblock%}
如果我忘记提到一个文件,请告诉我从
PostForm.Meta.widgets
中删除author