Django - 表单上没有显示模板的按钮

时间:2016-04-18 04:38:32

标签: python django forms templates django-forms

我一直在尝试一切,但我无法弄清楚它为什么没有出现。我在这个网站上到处搜索都没有运气。按钮显示但是当我点击它时,它在get_profile上给我一个TypeError,说:

例外值:
get_profile()得到了一个意外的关键字参数'用户名'

这是我的代码:

models.py

from django import forms
from apps.accounts.models import CustomUser


class RegistrationForm(forms.ModelForm):
    email = forms.EmailField(widget=forms.TextInput, label='email')
    username = forms.CharField(widget=forms.TextInput, label='username')
    password1 = forms.CharField(widget=forms.PasswordInput, label='Enter your password')
    password2 = forms.CharField(widget=forms.PasswordInput, label='Re-type your password')
    first_name = forms.CharField(widget=forms.TextInput, label='First Name')
    last_name = forms.CharField(widget=forms.TextInput, label='Last Name')

    class Meta:
        model = CustomUser
        fields = ['email', 'username', 'password1', 'password2', 'first_name', 'last_name']

    def clean(self, password1, password2):
        cleaned_data = super(RegistrationForm, self).clean()
        if password1 in self.cleaned_data and password2 in self.cleaned_data:
            if self.cleaned_data['password1'] != self.cleaned_data['password2']:
                raise forms.ValidationError("Passwords don't match. Please enter both fields again")
        return self.cleaned_data

    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

forms.py

def register(request):
    """
    User Registration View
    """
    if request.method == 'POST':
        form = RegistrationForm(request.POST)
        if form.is_valid():
            CustomUser = form.save()
            CustomUser.save()
            return redirect('home.html')
    else:
        form = RegistrationForm()
    return render_to_response('register.html', {
        'form': form,
    }, context_instance=RequestContext(request))

def get_profile(request):
    username = CustomUser.username
    return render_to_response(request, 'profile.html', {'username': username})

views.py

urlpatterns = patterns ('', 
    url(r'register$', 'apps.accounts.views.register', name='register'),)

urls.py

{% load staticfiles %}
{% load crispy_forms_tags %}

<!DOCTYPE html>
<html lang="en">
<body>
{% block body %}

<form method='POST' action="register" enctype= 'multipart/form-data'>{% csrf_token %}
<table>{{ form.ast_table}}</table>
<input type='submit' class="btn btn-default" value="Register" />
</form>
{% endblock %}
</body>
</html>

register.html

NinjaDomain.Classes

2 个答案:

答案 0 :(得分:1)

您似乎没有get_profile视图功能的正确签名。

你应该检查你的网址,你可能有类似的东西

url(r'^profile/(?P<username>\w+/$', get_profile),

如果是,您的观点应为

def get_profile(request, username):
    #you can get the user
    user = CustomUser.objects.get(username=username)

    return render_to_response(request, 'profile.html', {'username': username, 'user': user}) 

答案 1 :(得分:0)

CustomUser是班级。您需要在实际获取其用户名之前获取实例。

例如:

CustomUser.objects.get(pk=1).username

为您提供第一个用户的用户名。

此外,它应该是{{form.as_table}}而不是ast_table