Python 将用户名链接到用户创建的帖子
我想链接的用户谁创造了他们的用户名,这样人们就知道谁是张贴的,但我似乎无法让它工作,是的,我登录,我有一个工作的登记和登录表单已经 每次我登录时从表单中提交一些新闻时,我都会收到以下错误Python 将用户名链接到用户创建的帖子,python,django,authentication,Python,Django,Authentication,我想链接的用户谁创造了他们的用户名,这样人们就知道谁是张贴的,但我似乎无法让它工作,是的,我登录,我有一个工作的登记和登录表单已经 每次我登录时从表单中提交一些新闻时,我都会收到以下错误非空约束失败:news\u news.author\u id models.py from django.db import models from django.contrib.auth.models import User from markdownx.models import MarkdownxField
非空约束失败:news\u news.author\u id
models.py
from django.db import models
from django.contrib.auth.models import User
from markdownx.models import MarkdownxField
from markdownx.utils import markdownify
from taggit.managers import TaggableManager
class News(models.Model):
author = models.ForeignKey(User, unique=True, on_delete=models.CASCADE)
title = models.CharField(max_length=150)
short_desc = models.CharField(max_length=500)
content = MarkdownxField()
tags = TaggableManager()
slug = models.SlugField(unique=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return self.title
views.py
from django.shortcuts import render, redirect, get_object_or_404
from .models import Category, News
from .forms import NewNewsForm
from django.shortcuts import render
def show_news_view(request):
news = News.objects.values('author', 'title', 'short_desc', 'tags', 'created_at', 'updated_at')
context = {
'news': news
}
return render(request, "news/news_home.html", context)
def new_news_form_view(request):
if request.method == "POST":
form = NewNewsForm(request.POST or None)
if form.is_valid():
form.save()
form = NewNewsForm()
return redirect('/news')
else:
form = NewNewsForm()
context = {
'form': form
}
return render(request, "news/news_form.html", context)
编辑:
forms.py
from django import forms
from .models import News
class NewNewsForm(forms.ModelForm):
class Meta:
model = News
fields = ['title', 'short_desc', 'content', 'category', 'tags', 'slug']
news_form.html
{% extends "base.html" %}
{% block content %}
{% if user.is_authenticated %}
<form action="" method="POST">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Submit</button>
</form>
{% else %}
<p>Please login before you can submit a news story.</p>
{% endif %}
{% endblock content %}
{%extends“base.html”%}
{%block content%}
{%if user.u经过身份验证%}
{%csrf_令牌%}
{{form.as_p}}
提交
{%else%}
请先登录,然后才能提交新闻报道
{%endif%}
{%endblock内容%}
您没有通过用户
稍后使用更简单的解决方案进行编辑:
保存表单时只需传递commit=False
。它将创建您的新闻
对象,而不将其提交给DB。只需随后设置作者并保存对象
def new_news_form_view(request):
if request.method == "POST":
form_data = request.POST or None
form = NewNewsForm(form_data)
if form.is_valid():
news = form.save(commit=False)
news.author = request.user
news.save()
return redirect('/news')
else:
form = NewNewsForm()
context = {
'form': form
}
return render(request, "news/news_form.html", context)
我们需要查看
newnewform()
的声明,它看起来好像在保存表单之前没有设置属性。在我测试表单之前,表单工作正常。然后,我将author字段添加到表单中。现在,我发现了我在帖子中提到的错误@ViaTech@Brain等一下<代码>字段=['title','short_desc','content','category','tags','slug']在您的模型表单中没有作者id
这是您的问题,这就是为什么您明显地得到错误的原因。。。如果您也发布您的新闻
模型,则可能会显示该字段为必填字段,从而显示错误。简单地说,在form.isValid()
之后,将author\u id
分配给对象,然后保存它,您的错误应该可以解决。知道吗,但我得到了这个错误:name'user'未定义
我在哪里向forms.py或views.py文件添加最后一位代码?最后一位代码放在forms.py中。你是从哪里得到这个错误的?好的,我是这么想的,但是这个错误会在表单被删除后显示出来submitted@Brian刚刚用一个更简单的解决方案更新了我的答案。我想这会解决你的问题。