我对Django非常陌生,部分工作是创建自己的博客模型表单。我遵循了youtube教程的先前示例,但是无法创建自己的博客模型表单。该表单不会保存,因为它无效,并且我已经花了2天时间解决该问题,但找不到错误的地方。
此外,每次我提交表单时,它会将我重定向到另一个我不太了解原因的页面。
请帮助。
我正在使用Django 2.0.7版。
我的模型。py
from django.db import models
class Article(models.Model):
title = models.CharField(max_length=120) # Max length required
content = models.TextField()
active = models.BooleanField(default=True)
我的forms.py
from django import forms
from .models import Article
class ArticleForm(forms.ModelForm):
class Meta:
model = Article
fields = [
'title',
'content',
'active'
]
我的views.py
from django.shortcuts import render, get_object_or_404, redirect
from .forms import ArticleForm
from .models import Article
def article_list_view(request):
print("this is article_list.html")
queryset = Article.objects.all()
context ={
"object_list": queryset
}
return render(request, "blogs/article_list.html", context)
def article_create_view(request):
form = ArticleForm(request.POST or None)
print(ArticleForm.errors)
if form.is_valid():
print("Valid form")
form.save()
else:
print("invalid")
print(ArticleForm.errors)
context = {
"form": form
}
return render(request, "blogs/article_create.html", context)
article_create.html
{% extends 'base.html' %}
{% block content %}
<form action='.' method='POST'>{% csrf_token %}
{{ form.as_p }}
<input type='submit' value='Save'>
</form>
{% endblock %}
urls.py
from django.contrib import admin
from django.urls import include, path
from pages.views import (
home_view,
contact_view,
about_view,
social_view
)
from blog.views import (
article_list_view,
article_detail_view,
article_create_view
)
urlpatterns = [
path('products/', include('products.urls')),
path('', home_view, name='home'),
path('contact/', contact_view, name='contact'),
path('about/', about_view, name='product-detail'),
path('social/', social_view, name='social'),
path('admin/', admin.site.urls),
#blog paths
path('articles/', article_list_view, name = 'articles'),
path('articles/detail/<int:id>', article_detail_view, name = 'article-detail'),
path('articles/create', article_create_view, name = 'article-create'),
]
这显示在服务器的命令提示符下:
Django version 2.0.7, using settings 'trydjango.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
<property object at 0x000001ED94386458>
invalid
<property object at 0x000001ED94386458>
[18/Apr/2019 22:33:51] "GET /articles/create HTTP/1.1" 200 920
this is article_list.html
[18/Apr/2019 22:34:02] "POST /articles/ HTTP/1.1" 200 668
一旦我进入article / create或article_create.html,就会出现<property object at 0x000001ED94386458>
。
答案 0 :(得分:0)
1)您不应在类上使用.errors
。
ArticleForm.errors
应该是
form.errors
2)您应该在视图中分离GET和POST请求。
def article_create_view(request):
if request.method == "POST":
form = ArticleForm(request.POST)
if form.is_valid():
print("Valid form")
form.save()
else:
print("Invalid form")
print(form.errors)
else:
form = ArticleForm()
context = {"form": form)
return render(request, "blogs/article_create.html", context)
答案 1 :(得分:0)
您的表单应该发布到""
,而不是"."
。
<form action='' method='POST'>{% csrf_token %}
或者,请确保您最初导航到/articles/create/
,而不是没有最后斜杠的/articles/create
。 (尽管通常,内置的CommonMiddleware将负责使用斜杠重定向到路径-您是否有将APPEND_SLASH设置设置为False的可能性?)