简体   繁体   English

无法获取要在Django中呈现的表单

[英]Can't get forms to render in Django

I'm working on a project and I don't get the django forms to render on any of my pages. 我正在做一个项目,但没有Django表单在我的任何页面上呈现。 I've compared it to django girls code, as that is what I usually consult but it looks virtually identical to it. 我已将其与django girls代码进行了比较,因为这是我通常会参考的内容,但实际上与之相同。 It's not just this page, my other pages have issues with rendering the forms as well. 不只是此页面,我的其他页面在呈现表单时也存在问题。 Here's the code: 这是代码:

Views.py Views.py

from django.shortcuts import render
from .models import *
from .forms import *
from django.shortcuts import render, get_object_or_404
from django.shortcuts import redirect
from django.contrib.auth.decorators import login_required
from django.contrib.auth import login, authenticate
from django.contrib.auth.forms import UserCreationForm
from django.db.models import Sum
from django.utils import timezone
from django.views.decorators.http import require_POST
from .cart import Cart
from django.db import transaction
from django.contrib import messages


@login_required
def post_edit(request, pk):
    post = get_object_or_404(Post, pk=pk)
    if request.method == "POST":
        form = PostForm(request.POST, instance=post)
        if form.is_valid():
            post = form.save(commit=False)
            post.save()
            return redirect('post_detail', pk=post.pk)
    else:
        form = PostForm(instance=Post)
    return render(request, 'rentadevapp/post_edit.html', {'rentadevapp': post_edit}, {'form': form})

Forms.py Forms.py

class PostForm(forms.ModelForm):

    class Meta:
        model = Post
        fields = ('title', 'text',)

post_edit.html post_edit.html

{% extends 'rentadevapp/base.html' %}
{% load staticfiles %}
{% load crispy_forms_tags %}
{% block content %}
<head>
    <link rel="stylesheet" href="{% static 'css/post_edit.css' %}">
</head>
<body>
    <div class="container"><br>
        <h2>New Post</h2><br>
        <form method="POST" class="post-form">{% csrf_token %}
            {{ form.as_p }}
            <button type="submit" class="save btn btn-default">Save</button>
        </form>
    </div>
</body>
{% endblock %}

Models.py Models.py

class Post(models.Model):
    author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    title = models.CharField(max_length=200)
    text = models.TextField()
    created_date = models.DateTimeField(
        default=timezone.now)
    updated_date = models.DateTimeField(auto_now_add=True)
    price = models.DecimalField(max_digits=10, decimal_places=2, default='0')

    class Meta:
        ordering = ('title',)

    def created(self):
        self.created_date = timezone.now()
        self.save()

    def updated(self):
        self.updated_date = timezone.now()
        self.save()

    def publish(self):
        self.published_date = timezone.now()
        self.save()

    def __str__(self):
        return self.title

I'm pretty stuck and have spent a couple hours trying to figure this out. 我很困,花了几个小时试图解决这个问题。 Any help is really appreciated. 任何帮助都非常感谢。 Thanks! 谢谢!

Your form isn't returned to the template in the context. 您的表单不会在上下文中返回到模板。

In Django 1.11 or 2.2 the render function call in your view should return a dictionary of context variables as the third argument, but you've got two dictionaries. 在Django 1.112.2中 ,视图中的render函数调用应返回上下文变量字典作为第三个参数,但是您有两个字典。 The 4th argument where you've got a dictionary containing the form is being passed as content_type which is then used in the HttpResponse so I'm quite surprised there isn't something strange happening or an error seen. 第四个参数(其中包含包含表单的字典)将作为content_type传递,然后在HttpResponse使用它,因此我很惊讶没有发生任何奇怪的事情或看到错误。

So you're doing; 所以你在做;

return render(request, 'rentadevapp/post_edit.html', {'rentadevapp': post_edit}, {'form': form})

What you need to do is; 您需要做的是;

context = {'form': form, 'rentadevapp': post_edit}

return render(request, 'rentadevapp/post_edit.html', context)

Prior to 1.10 render had a different signature, but the first three arguments of request, template_name, context have been that way since <1.8 在1.10之前, render具有不同的签名,但是自<1.8以来, request, template_name, context的前三个参数request, template_name, context一直是这种方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM