将用户名链接到用户创建的帖子

时间:2019-10-16 22:32:13

标签: python django authentication

我想通过用户名链接创建帖子的用户,这样人们就可以知道该帖子的发布者,但我似乎无法正常工作,是的,我已经登录并且拥有有效的注册信息,登录表单。

每次登录时,我都会从表单提交一些新闻,但都会收到此错误NOT NULL constraint failed: news_news.author_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 %}

1 个答案:

答案 0 :(得分:0)

您没有通过用户。

使用更简单的解决方案进行后期编辑:

保存表单时只需传递create function uf_Financial_RATE (@nper int, @pmt float, @pv float, @fv float, @type bit) returns float as begin declare @maxIterations int, @i int; declare @rate float, @y float, @slope float; declare @epsilon float, @guess float; declare @pow float, @pow1 float; -- Test for rate = 0 select @y = (@pmt * @nper) + @pv + @fv; if (@y <= @epsilon and @y >= -@epsilon) return 0; -- Use Newton method of approximating the root select @maxIterations = 20, @epsilon = 0.00001, @guess = 0.1 select @i = 0, @rate = @guess; while @i < @maxIterations begin select @pow = POWER(1 + @rate, @nper), @pow1 = POWER(1 + @rate, @nper - 1); -- Find y = f(rate) from excel formula select @y = @pv * @pow + @pmt * (1 + @rate * @type) * ((@pow - 1) / @rate) + @fv; -- Check if precission is reached if @y <= @epsilon and @y >= -@epsilon return @rate; -- Find slope value ( y' = f'(rate) ) from the derivative of excel formula select @slope = @pv * @nper * @pow1 + @pmt * ((@type * (@pow - 1) / @rate) + (1 + @rate * @type) * (((@rate * @nper * @pow1) - (@pow - 1)) / POWER(@rate, 2))); -- New guess rate select @rate = -@y / @slope + @rate; -- Next iteration select @i = @i + 1; end -- Could not solve return -1; end 。它将创建您的commit=False对象,而不将其提交给DB。之后只需设置作者并保存对象即可。

News