gpt4 book ai didi

python - Django 中的表单预览

转载 作者:行者123 更新时间:2023-12-05 02:07:14 27 4
gpt4 key购买 nike

我正在构建注册表单。每当用户填写表格并单击注册按钮时,我希望他们看到他们提交的预览。我对论点有疑问。这是我的代码:

模型.py

from django.db import models
from phonenumber_field.modelfields import PhoneNumberField

# Create your models here.
class Register(models.Model):
regChoice = (
('Self', 'Self'),
('Group', 'Group'),
('Corporate', 'Corporate'),
('Others', 'Others'),
)
name = models.CharField(max_length=50)
email = models.EmailField(max_length=254,null=True)
phoneNumber = PhoneNumberField(null=True)
idCard = models.ImageField(null=True)
regType = models.CharField(max_length=25, choices=regChoice,null=True)
ticketNo = models.IntegerField(default=1)

def __str__(self):
return self.name

表格.py

from django import forms
from django.forms import ModelForm
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from .models import *

class RegisterForm(ModelForm):
name = forms.CharField(widget=forms.TextInput(attrs={'placeholder':'Your full name...'}))
email = forms.CharField(widget=forms.TextInput(attrs={'placeholder':'Your email...'}))
phoneNumber = forms.CharField(widget=forms.TextInput(attrs={'placeholder':'Your phone number...'}))

class Meta:
model = Register
fields = '__all__'

网址.py

from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='home'),
path('preview.html/<str:pk>', views.preview, name="preview")
]

View .py

from django.shortcuts import render, redirect
from .models import *
from .forms import *
# Create your views here.
def index(request):
form = RegisterForm()
if request.method == 'POST':
form = RegisterForm(request.POST, request.FILES)
if form.is_valid():
form.save()
context = {'form':form}
return render(request, 'event/index.html', context)

def preview(request, pk):
reg = Register.objects.get(id=pk)
prev = RegisterForm(instance=reg)
if request.method == 'POST':
form = RegisterForm(request.POST, instance=reg)
if form.is_valid():
form.save()
return redirect('/')
context = {'reg':reg, 'prev':prev}
return render(request, 'event/preview.html', context)

index.html

{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Event Registration</title>
<link rel="stylesheet" href="{% static 'css/style.css' %}">
<script src="{% static 'js/script.js' %}"></script>
</head>
<body>
<div class="mobile-screen">
<div class="header">
</div>

<div class="logo"></div>

<form id="login-form" method="POST" action="{% url 'preview' form.id %}" enctype="multipart/form-data">
{% csrf_token %}
{{form.name}}
{{form.email}}
{{form.phoneNumber}}
<legend style="color: aliceblue;">Upload ID card: </legend>{{form.idCard}}
<div style="text-align: center; color: aliceblue;">Registration Type: {{form.regType}}</div>
{{form.ticketNo}}
<input class="btn btn-sm btn-primary" type="submit" value="Register" name="Register">
</form>

</div>
</body>
</html>

预览.html

Hello {{prev.name}},
your email is {{prev.email}}
your phone number is {{prev.phoneNumber}}
your idCard photo is {{prev.idCard.url}}
your registration type is {{prev.regType}}
your number of tickets is {{prev.ticketNo}}

我遇到的错误是:

NoReverseMatch at /

Reverse for 'preview' with arguments '('',)' not found. 1 pattern(s) tried: ['preview\.html/(?P[^/]+)$']

最佳答案

当有人到达您的索引页面并输入我们需要的表单时

  1. 将表单作为 POST 请求提交到索引 View
  2. 保存表单从而在数据库中创建模型
  3. 使用上述 id 将用户重定向到预览 View

要做到这一点,代码需要有点像这样,我还没有测试过,但你应该明白了。

from django.shortcuts import redirect

def index(request):
form = RegisterForm()
if request.method == 'POST':
form = RegisterForm(request.POST, request.FILES)
if form.is_valid():
instance = form.save()
return redirect('preview', pk=instance.id)
context = {'form':form}
return render(request, 'event/index.html', context)

在您的 index.html 更改中

action="{% url 'preview' form.id %}"

action=""

因为我们希望它发布到 INDEX View ,因为那是 POST 处理逻辑所在的地方。

然后索引 View 重定向到使用新生成的对象进行预览。

另外正如@Snir 在另一个答案中提到的那样,在 URLS 中包含 .html 并不是标准做法。最好简单地把它做成这样:

path('preview/<str:pk>', views.preview, name="preview")

关于python - Django 中的表单预览,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61998149/

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