【发布时间】:2021-03-12 13:39:37
【问题描述】:
我在 Django 中创建了一个客户模型,但无法将数据保存到数据库表中,当我单击注册按钮时,它只会将我重定向回主页。 我在客户模型和注册功能下方附上了。 ...请帮我解决问题。 客户.py
from django.db import models
class Customer(models.Model):
first_name= models.CharField(max_length=50)
last_name= models.CharField(max_length=50)
phone=models.CharField(max_length=15)
email=models.EmailField()
password=models.CharField(max_length=500)
def register(self):
self.save()
views.py
def signup(request):
if request.method == 'GET':
return render(request, 'signup.html')
else:
postData=request.POST
first_name=postData.get('firstname')
last_name=postData.get('lastname')
phone=postData.get('phone')
email=postData.get('email')
password=postData.get('password')
print(first_name,last_name,phone,email,password)
customer=Customer(first_name=first_name,last_name=last_name,phone=phone,email=email,password=password)
customer.register()
return HttpResponse("Signup success")
signup.html
{% extends 'base.html' %}
{% block content %}
<div class="container">
<div class="p-4 m-4">
<div class="col-lg-5 mx-auto border rounded pt-4">
<h3 class="alert alert-light border rounded" >Create An Account</h3>
<form action="/" method="POST">
{% csrf_token %}
<!--firstname-->
<div class="form-group">
<label for="">First Name</label>
<input type="text" name="firstname" id="" class="form-control form-control-sm" placeholder="Mike">
</div>
<!--lastname-->
<div class="form-group">
<label for="">Last Name</label>
<input type="text" name="lastname" id="" class="form-control form-control-sm" placeholder="Ross">
</div>
<!--phone-->
<div class="form-group">
<label for="">Phone No</label>
<input type="text" name="phone" id="" class="form-control form-control-sm" placeholder="9876543210">
</div>
<!--email-->
<div class="form-group">
<label for="">Email</label>
<input type="email" name="email" id="" class="form-control form-control-sm" placeholder="abc@gmail.com">
</div>
<!--password-->
<div class="form-group">
<label for="">Password</label>
<input type="password" name="password" id="" class="form-control form-control-sm" placeholder="********">
</div>
<div class="form-group">
<input class="btn btn-sm btn-info" type="submit" value="Sign Up">
</div>
</form>
</div>
</div>
</div>
{% endblock %}
【问题讨论】:
-
我希望这个注册页面的 URL 对应于您在表单上放置的 action 属性的值?
-
我已经这样保存了........from django.contrib import admin from django.urls import path from .views import index, signup urlpatterns = [ path('', index ), path('signup', signup), ]
标签: html django django-models django-views django-forms