我可能遗漏了一些非常简单的东西,因为我的模型表单没有显示在模板上。代码非常简单:
models.py:
class Story(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL)
title = models.CharField(max_length=200)
content = models.TextField()
picture = models.ImageField(upload_to = 'images/post_images')
date = models.DateTimeField(auto_now_add=True)
forms.py:
class StoryForm(forms.ModelForm):
class Meta:
model = Story
views.py:
from sfv.forms import StoryForm
@login_required(redirect_field_name=None)
def restricted(request):
user = request.user
form = StoryForm()
#graph = get_persistent_graph(request)
return render(request, "restricted.html", {user : 'user', form : 'form',})
模板:
<form method = 'POST' action = ''>
<table>
{{ form }}
<table>
</form>
我也试过form.as_p
,但没有帮助。
答案 0 :(得分:0)
从
更改render
方法
return render(request, "restricted.html", {user : 'user', form : 'form',})
到
return render(request, "restricted.html", { 'user' : user, 'form' : form})
上下文字典中的key
和value
已互换。因此问题。