我想做一件非常简单的事情,但由于某种原因我还没有找到解决方案。我有一个 HTML 表单
<form id="user_form" method="POST" action="/ProjectName/home/">
{% csrf_token %}
<div class="registerLabel">
Full name:
</div>
<div class="registerTextLabel">
<input type="text" id="registerFullName" class="registerTextDetails">
</div>
<div class="registerLabel">
Username:
</div>
<div class="registerTextLabel">
<input type="text" id="registerUsername" class="registerTextDetails">
</div>
<div id="registerButtonDiv">
<button class="registerButtons" id="cancelRegisterButton">Cancel</button>
<button type="submit" class="registerButtons" id="submitRegistration">Register</button>
</div>
</form>
我想要的是在填写此表单并提交它以获取所有文本框值并将其放入我的数据库中之后。
db 模型是这样实现的:
class User(models.Model):
username = models.CharField(max_length=200)
password = models.CharField(max_length=200)
...
def __unicode__(self):
return self.username
这是视图的代码:
def register(request):
context = RequestContext(request)
registed = False
if request.method == 'POST':
form = User(data = request.POST)
username = request.POST.get('registerUsername')
password = request.POST.get('registerPass')
user = form.save()
username = User.username
password = User.password
u = User(username,password,...)
u.save()
registered = True
else:
form = User()
return render_to_response('ProjectName/home.html', {'user_form':User}, context)
我不想使用 Django 从模型生成表单的自动化方式。