无法让 'Form' 在 Django 中工作
Can't get 'Form' working in Django
我正在使用 Django/Python 创建网站。我刚刚开始注册页面,用户将在其中输入他们的信息,数据应该流入 Django 附带的 sqlite3 数据库。从昨天晚上开始(现在不到 24 小时),我一直在试图弄清楚为什么我的表格没有出现。我尝试附加我得到的东西与我的目标,但没有足够的声誉点所以
请访问:https://www.youtube.com/watch?v=yfjhLKL-_5Q 9.09 分钟了解我的目标。请注意:YouTube 视频中的 'welcome' 已被我替换为 'Thank you'。此代码在我的 views.py 中。
问题是用户在视频中输入全名和电子邮件的小框(我已将其替换为职业、first_name、last_name 等)没有出现在所有... 相反,我只是 'Thank you' 与我的 'username' (等效于 jmitchel3)和注册按钮一起出现,但没有请求文本框。我的代码如下。
我最初认为我的表单设置不正确,所以我将我的代码与成功但失败的页面进行了比较。我对模型做了同样的事情,但也看不到任何东西......
models.py:
from django.db import models
# Create your models here.
class Register(models.Model):
Profession = models.CharField(max_length=120)
first_name = models.CharField(max_length=120)
last_name = models.CharField(max_length=120)
email = models.EmailField(max_length = 120, unique = True)
phone_number = models.CharField(max_length=120)
instgram_ID = models.CharField(max_length=120, blank=True, null=True)
facebook_ID = models.CharField(max_length=120, blank=True, null=True)
twitter_ID = models.CharField(max_length=120, blank=True, null=True)
timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
updated = models.DateTimeField(auto_now_add=False, auto_now=True)
def __unicode__(self):
return self.email
forms.py
from django import forms
from .models import Register
class RegisterForm(forms.ModelForm):
class Meta:
model = Register
fields = ['first_name', 'last_name', 'email', 'phone_number', 'instgram_ID', 'facebook_ID', 'twitter_ID']
def clean_email(self):
email = self.cleaned_data.get('email')
email_base, provider = email.split("@")
domain, extension = provider.split('.')
#if not domain = 'USC':
# raise forms.ValidationError("Please make sure you use your USC email.")
#if not extension == "edu":
# raise forms.ValidationError("Please use a valid .EDU email address")
#return email
def clean_full_name(self):
full_name = self.cleaned_data.get('full_name')
#write validation code.
return full_name
admin.py
from django.contrib import admin
# Register your models here.
from .forms import RegisterForm
from .models import Register
class RegisterAdmin(admin.ModelAdmin):
list_display = ["__unicode__", "timestamp", "updated"]
form = RegisterForm
#class Meta:
# model = Register
)
admin.site.register(Register, RegisterAdmin
views.py
from django.shortcuts import render
from .forms import RegisterForm
# Create your views here.
def home(request):
title = 'Welcome'
form = RegisterForm(request.POST or None)
context = {
"title": title,
"form": form
}
if form.is_valid():
instance = form.save(commit=False)
if not instance.full_name:
instance.full_name = "Justin"
instance.save
context = {
"title": "Thank you"
}
return render(request, "home.html", context)
home.html:
<h1>{{title}}</h1>
{{ user }}
{{ request.user }}
<form method ='POST' action=''>{% csrf_token %}
{{ form }}
<input type='submit' value = 'Register'>
<!-- <input> type='text'> -->
<!-- <input> -->
</form>
我已经有一段时间没来这里了,所以如果我需要添加任何其他内容,请告诉我..
您有一个缩进错误。 context
被 {"title": "Thank you"}
替换的部分应该在 is_valid()
块中缩进。现在,它正在替换早期的上下文定义,结果是您没有将表单发送到模板。
我正在使用 Django/Python 创建网站。我刚刚开始注册页面,用户将在其中输入他们的信息,数据应该流入 Django 附带的 sqlite3 数据库。从昨天晚上开始(现在不到 24 小时),我一直在试图弄清楚为什么我的表格没有出现。我尝试附加我得到的东西与我的目标,但没有足够的声誉点所以 请访问:https://www.youtube.com/watch?v=yfjhLKL-_5Q 9.09 分钟了解我的目标。请注意:YouTube 视频中的 'welcome' 已被我替换为 'Thank you'。此代码在我的 views.py 中。
问题是用户在视频中输入全名和电子邮件的小框(我已将其替换为职业、first_name、last_name 等)没有出现在所有... 相反,我只是 'Thank you' 与我的 'username' (等效于 jmitchel3)和注册按钮一起出现,但没有请求文本框。我的代码如下。
我最初认为我的表单设置不正确,所以我将我的代码与成功但失败的页面进行了比较。我对模型做了同样的事情,但也看不到任何东西......
models.py:
from django.db import models
# Create your models here.
class Register(models.Model):
Profession = models.CharField(max_length=120)
first_name = models.CharField(max_length=120)
last_name = models.CharField(max_length=120)
email = models.EmailField(max_length = 120, unique = True)
phone_number = models.CharField(max_length=120)
instgram_ID = models.CharField(max_length=120, blank=True, null=True)
facebook_ID = models.CharField(max_length=120, blank=True, null=True)
twitter_ID = models.CharField(max_length=120, blank=True, null=True)
timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
updated = models.DateTimeField(auto_now_add=False, auto_now=True)
def __unicode__(self):
return self.email
forms.py
from django import forms
from .models import Register
class RegisterForm(forms.ModelForm):
class Meta:
model = Register
fields = ['first_name', 'last_name', 'email', 'phone_number', 'instgram_ID', 'facebook_ID', 'twitter_ID']
def clean_email(self):
email = self.cleaned_data.get('email')
email_base, provider = email.split("@")
domain, extension = provider.split('.')
#if not domain = 'USC':
# raise forms.ValidationError("Please make sure you use your USC email.")
#if not extension == "edu":
# raise forms.ValidationError("Please use a valid .EDU email address")
#return email
def clean_full_name(self):
full_name = self.cleaned_data.get('full_name')
#write validation code.
return full_name
admin.py
from django.contrib import admin
# Register your models here.
from .forms import RegisterForm
from .models import Register
class RegisterAdmin(admin.ModelAdmin):
list_display = ["__unicode__", "timestamp", "updated"]
form = RegisterForm
#class Meta:
# model = Register
)
admin.site.register(Register, RegisterAdmin
views.py
from django.shortcuts import render
from .forms import RegisterForm
# Create your views here.
def home(request):
title = 'Welcome'
form = RegisterForm(request.POST or None)
context = {
"title": title,
"form": form
}
if form.is_valid():
instance = form.save(commit=False)
if not instance.full_name:
instance.full_name = "Justin"
instance.save
context = {
"title": "Thank you"
}
return render(request, "home.html", context)
home.html:
<h1>{{title}}</h1>
{{ user }}
{{ request.user }}
<form method ='POST' action=''>{% csrf_token %}
{{ form }}
<input type='submit' value = 'Register'>
<!-- <input> type='text'> -->
<!-- <input> -->
</form>
我已经有一段时间没来这里了,所以如果我需要添加任何其他内容,请告诉我..
您有一个缩进错误。 context
被 {"title": "Thank you"}
替换的部分应该在 is_valid()
块中缩进。现在,它正在替换早期的上下文定义,结果是您没有将表单发送到模板。