2

我想做一件非常简单的事情,但由于某种原因我还没有找到解决方案。我有一个 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 从模型生成表单的自动化方式。

4

3 回答 3

1

您忘记在表单中定义名称。获取 POST 数据很重要。

<form .........>
   {{error}}<br/><br/>
<div class="registerLabel">Username:</div>
<div class="registerTextLabel">
    <input type="text" id="registerUsername" name="registerUsername" class="registerTextDetails">
</div>

<div class="registerLabel" id="registerLabelPass">Password:</div>
<div class="registerTextLabel">
    <input type="password" id="registerPass" name="registerPass" class="registerTextDetails">
</div>
</form>

def register(request):
    context = RequestContext(request)
    registered = False
    error = ''
    if request.method == 'POST':
        username = request.POST.get('registerUsername')
        password = request.POST.get('registerPass')

        if username and password:
            username_exist = User.objects.filter(username=username)
            if username_exist:
                error = username is already taken, try another
            else:
                User.objects.create(username=username, password=password)
                registered = True
        else:
            error = all fields are required

    return render_to_response('ProjectName/home.html', {'error':error}, context)
于 2013-03-10T15:39:43.807 回答
0

这个用户名不应该 = User.username 密码 = User.password

User.username = 用户名 User.password = 密码

于 2016-10-22T02:43:18.403 回答
0

这是我的 html 文件:

{% block content %}
    <div class="container">
        <h6 class="dress"><a href="{% url 'home' %}">Home</a> <i> </i> Register </h6>
    </div>
    <div class="back">
        <h2>PRODUCTS</h2>
    </div>
     <div class="container">
        <div class="register">
        <h3>PERSONAL INFORMATION</h3>
              <form method='POST' action=''>{% csrf_token %}
                    <div class="mation">
                    <div>
                        <span>User Name</span>
                        <input id="id_username" maxlength="150" name="username" type="text" required />
                        <span class="helptext">Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.</span></p>
                    </div>
                     <div>
                         <span>Email Address</span>
                         <input id="id_email" maxlength="254" name="email" type="text" />
                         </div>
                         <div>
                            <span>Password</span>
                                <input id="id_password1" name="password1" type="password" required />
                    </div>
                             <div>
                            <span>Confirm Password</span>
                                <input id="id_password2" name="password2" type="password" required />
                    </div>
                     </div>
                     <input type="submit" value="Register">
                </form>


           </div>
            </div>
{% endblock %}

现在您可以看到我的 RegisterFrom:

class RegistrationForm(forms.ModelForm):
    password1 = forms.CharField(label="Password", widget=forms.PasswordInput())
    password2 = forms.CharField(label="Confirm Password", widget=forms.PasswordInput())

    class Meta:
        model = User
        fields = ['username', 'email']

    def clean_password2(self):
        password1 = self.cleaned_data.get("password1")
        password2 = self.cleaned_data.get("password2")

        try:
            if ((password1 and password2) and (password1 != password2)):
                raise forms.ValidationError("Password Do not Match")
        except:
            pass
        return password2

    def save(self, commit=True):
        user = super(RegistrationForm, self).save(commit=False)
        user.set_password(self.cleaned_data['password1'])
        if commit:
            user.save()
        return user

像这样我在 Django 中使用自定义文本框

于 2017-04-04T16:06:58.900 回答