gpt4 book ai didi

python - Django 表单未在模板中上传

转载 作者:行者123 更新时间:2023-12-01 08:51:22 24 4
gpt4 key购买 nike

Django 表单没有在索引页中渲染,但是当我创建一个新的 html 文件时它就可以工作了

forms.py

class UserRegisterForCourse(forms.ModelForm):
class Meta:
model = RegisterForCourse
fields = ['first_name', 'last_name', 'subject', 'phone']

view.py

def about_course(request):
if request.method == 'POST':
form = UserRegisterForCourse(request.POST)
if form.is_valid():
form.save()
subject = form.cleaned_data.get('subject')
messages.success(request, f'you have been successfully registered for {subject}')
return redirect('index')
else:
form = UserRegisterForCourse()
return render(request, 'goal/index.html', {'form':form})

在index.html

<form action="{% url 'about' %}" method="POST" role="form">
{% csrf_token %}
{{ form }}
</form>

有什么想法吗?它需要在索引页中工作我缺少什么?

最佳答案

请检查以下流程

forms.py

from django.contrib.auth.models import User
from django import forms

class UserForm(forms.ModelForm): #Classname

class Meta:
model = User
fields = ['first_name', 'last_name']

在views.py

from django.contrib.auth.models import User
from .forms import UserForm # Add form which is written in your forms.py
def index(request):
form = UserForm() #Initiate
return render(request, 'index.html', {'form': form})

index.html

<form action="{% url 'about' %}" method="POST" role="form">
{% csrf_token %}
{{ form }}
</form>

关于python - Django 表单未在模板中上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53097268/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com