因此,我刚开始使用django,并正在尝试实现ModelForm。但是出了点问题,来自模型的输入字段没有出现在HTML中,但是在HTML中实现的提交按钮却出现了。如果有人可以告诉我我做错了什么,或者可以将我推荐给可以帮助我的消息来源,我将为您提供帮助。
form.py
from .models import Article
from django import forms
class Article_form(forms.ModelForm):
class Meta:
model = Article
fields = [
'title',
'content'
]
models.py
from django.db import models
# Create your models here.
class Article(models.Model):
title = models.CharField(max_length = 120)
content = models.TextField(max_length = None)
views.py
from django.shortcuts import render
from .forms import Article_form
from .models import Article
# Create your views here.
def create_article(request):
form = Article_form(request.POST or None)
if form.is_valid():
form.save()
form = Article_form()
context = {
'form': form
}
return render(request, "articles/form.html", context)
form.html
<form method="POST">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="OK">
</form>
index.html
<html>
<body>
<h2>This is a page</h2>
{% include 'articles/form.html' %}
</body>
</html>
如果您需要其他任何信息,请告诉我! 谢谢!
编辑:
查看登陆页面
from django.shortcuts import render
# Create your views here.
def home_view(request):
return render(request, 'index.html/')
urls.py (我什至还没有碰过)
from django.contrib import admin
from django.urls import path
from pages import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home_view),