django创建用户后将其重定向到该帖子

时间:2020-08-31 17:57:32

标签: python django django-models django-forms django-views

我想在使用Django表单创建帖子后将用户重定向到该帖子

在models.py

class Text(models.Model):

    title = models.CharField(max_length=200, null=True)
    document = models.TextField(max_length=None, null=True)
    requirements = models.TextField(max_length=200, null=True)
    date_created = models.DateField(auto_now_add=True, null=True)
    deadline = models.DateField(null=True)

    author = models.ForeignKey(User, on_delete=models.CASCADE, null=True)

    def __str__(self):
        return self.title

在我看来。py

    if request.method == "POST":   
        form = TextForm(request.POST)
        if form.is_valid() and request.user.is_authenticated:
            user = request.user
            text = form.save(commit=False)
            author = User.objects.filter(email=user.email).first()
            text.author = author
            form.save()
            form = TextForm()

    context = {'form': form, 'text': text}

    return render(request, 'main/text.html', context)

在我的表格中。py

class TextForm(ModelForm):
    class Meta:
        model = Text
        fields = ['title','document','requirements','deadline']
        widgets = {
            'title' : forms.TextInput(attrs={'placeholder':'Title','class':'form-control m-2 mb-4 pb-2'}),
            'deadline' : forms.DateInput(attrs={'placeholder':'Deadline','type':'date','class':'form-control m-2 pt-2',
            'id':'opendate'}),
            'requirements' : forms.Textarea(attrs={'placeholder':ps_note,'class':'form-control col m-2','rows':'3'}),
            'document' : forms.Textarea(attrs={'placeholder':ps_text,'class':'form-control'}),

        }

在我的urls.py

from django.urls import path
from . import views

urlpatterns = [

    path('', views.home, name='home'),
    path('text/<str:pk>', views.text, name="text"),
    path('signin/', views.signin, name="signin"),
    path('signup/', views.signup, name="signup"),
    path('logout/', views.logout, name="logout"),
]
 

我必须添加此内容,以便stackoverflow接受我的问题,因为即使说了我认为的所有细节,它也说大部分是代码

4 个答案:

答案 0 :(得分:0)

我认为您必须使用“ commit”

form.save(commit=True)

检查此答案

enter link description here

答案 1 :(得分:0)

def text(request, pk):

    form = TextForm()
    
    if request.method == "POST":
        form = TextForm(request.POST)
        if form.is_valid():
            text = form.save(commit=False)
            text = Text.objects.get(id=pk)
            text.save()
            return redirect('text')
    else:
        text = Text.objects.get(id=pk)

    context = {'form': form, 'text': text}

    return render(request, 'main/text.html', context)

答案 2 :(得分:0)

如果有人正在寻找类似的东西,
几天后,我能够自己解决它,这就是我所做的:

if request.method == "POST":   
    text_form = TextForm(request.POST)
    if text_form.is_valid() and request.user.is_authenticated:
        user = request.user
        text = text_form.save(commit=False)
        author = User.objects.filter(email=user.email).first()
        text.author = author
        text_form.save()
        text_form = TextForm()
          # this to select the id of the model object
        pk = text.id
        return redirect("text",pk)

答案 3 :(得分:-1)

您不应该在urls.py中将“名称”更改为“ pk”吗?