这是我的第一个Django项目,我试图让用户在填写注册表单并点击注册按钮后被重定向到登录页面;然而,相反,我的注册表单只是重新加载,我仍然在我的signup.html页面。我不知道为什么会发生这种情况,任何帮助都将不胜感激。(注意-idk这是否相关-我的html文件在templates文件夹中,.js和.css文件在静态文件夹中,两者都是独立的,与我的应用程序文件夹处于同一级别)
(完整)signup.html:
{% load static%}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" >
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>FitFlow - Sign Up</title>
<link rel="stylesheet" type="text/css" href="{% static 'styles.css' %}">
<!--flatpickr-->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/flatpickr/dist/flatpickr.min.css">
</head>
<body class="page-background">
<div class = "container">
<form class="form" action="{% url 'signup' %}" method="POST">
{% csrf_token %}
<h1 class="form_title">Create Account</h1>
<div class="form_input_group">
<input type="text" id= "name" class = "form_input" autofocus placeholder="Name" required>
</div>
<div class="form_input_group">
<input type="text" id= "username" class = "form_input" placeholder="Username" required>
</div>
<div class="form_input_group">
<input type="email" id="email" class = "form_input" autofocus placeholder="Email" required>
</div>
<div class="form_input_group">
<input type="password" id="password1" class = "form_input" autofocus placeholder="Password" required>
<div class="form_input-error-message"></div>
</div>
<div class="form_input_group">
<input type="password" id ="password2" class = "form_input" autofocus placeholder="Confirm Password" required>
<div class="form_input-error-message"></div>
</div>
<div class="form_input_group">
<div class="custom_select">
<select id="ageRange" name="ageRange" required>
<option value="">Age Range</option>
<option value="1">under 18</option>
<option value="2">18-29</option>
<option value="3">30-39</option>
<option value="4">40-49</option>
<option value="5">50+</option>
</select>
</div>
</div>
<div class="form_input_group">
{% if error %}
<div class="error-message">
The passwords do not match.
</div>
{% endif %}
</div>
<div class="form_input_group">
<input type="text" class="custom-lastPeriod" id="lastPeriod" name="lastPeriod" autofocus placeholder="First Day of Last Period" required>
</div>
<div class="form_input_group">
<div class="custom_select">
<select id="exercisePurpose" name="exercisePurpose" required>
<option value="">Exercise Purpose</option>
<option value="loseWeight">Lose Weight</option>
<option value="buildMuscle">Build Muscle</option>
<option value="improveEndurance">Improve Endurance</option>
<option value="stayActive">Stay Active</option>
</select>
</div>
</div>
<div class="form_button_container">
<button class ="form_button" type="submit">Sign Up</button>
</div>
<p class="form_text">
<a href="{% url 'login' %}">Already have an account? Log in</a>
</p>
</form>
</div>
<!--Bootsrap JS
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/js/bootstrap.bundle.min.js" integrate ></script>-->
<script src="https://cdn.jsdelivr.net/npm/flatpickr"></script>
<script>
flatpickr("#lastPeriod", {
placeholder: "Select Last Period"
});
</script>
<script src="{% static 'script2.js' %}"></script>
</body>
</html>
字符串
网址:views.py
def login(request):
if request.method == "POST":
email = request.POST["email"]
password = request.POST["password1"]
user = authenticate(email=email, password=password)
if user is not None:
login(request, user)
return redirect('mainpg')
else:
return render(request, 'login.html', {"error": "Invalid username or password."})
return render(request, 'login.html')
def signup(request):
if request.method == "POST":
try:
username = request.POST['username']
except MultiValueDictKeyError as e:
messages.error(request, 'Please enter a username.')
return redirect('signup')
name = request.POST['name']
email = request.POST['email']
age_range = request.POST['ageRange']
last_period = datetime.strptime(request.POST['lastPeriod'], "%Y-%m-%d")
password1 = request.POST['password1']
password2 = request.POST['password2']
exercise_purpose = request.POST['exercisePurpose']
print("Sign up form submitted")
if password1!= password2:
return render(request, 'signup.html', {"error": "Passwords do not match."})
user = User.objects.create_user(username=username, name=name, email=email, password=password1)
user.save()
print("Sign up form submitted")
return redirect('/login')
return render(request, 'signup.html')
型
网址:urls.py
from django.contrib import admin
from django.urls import path, include
from . import views
from django.conf import settings
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns = [
path('', views.app, name="app"),
path('login/', views.login, name="login"),
path('signup/', views.signup, name="signup"),
path('mainpg/', views.mainpg, name="mainpg"),
path('forgotp', views.forgotp, name="forgotp"),
]
型
网址:settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
STATIC_ROOT = os.path.join(BASE_DIR, '..','collected_static')
型
1条答案
按热度按时间qpgpyjmq1#
字符串
这就是问题所在。此输入元素没有
name
属性。您试图使用id
,就好像它是name
一样。其他几个领域也是如此。
此外,表单上没有显示
{{messages}}
的地方,这就解释了为什么视图中的messages.error()
调用没有显示。