Django使用带有initial关键字的模型表单进行用户更新

时间:2017-04-27 23:11:00

标签: html django-models django-forms django-templates django-views

我尝试使用模型表单更新用户配置文件,方法是使用initial关键字传递已存储的用户详细信息。我不知道我的代码有什么问题。当我单击提交按钮时,它只是重新加载相同的页面而不是更新/保存详细信息。我需要一些帮助来解决这个问题。

## views.py ##

        def personal_details_update(request):
        if not request.user.is_authenticated():
            return HttpResponseRedirect('/login/')
        else:
            user = request.user
            data = {
                'username': user.username,
                'email': user.email,
                'first_name': user.first_name,
                'last_name': user.last_name,
                'address': user.address,
                'city': user.city,
                'state': user.state,
                'country': user.country,
                'mobile': user.mobile,
            }
            form = PersonalDetailsForm(request.POST or None, initial=data)
            if request.method == 'POST':
                if form.is_valid():
                    user.username = request.POST['username']
                    user.email = request.POST['email']
                    user.first_name = request.POST['first_name']
                    user.last_name = request.POST['last_name']
                    user.address = request.POST['address']
                    user.city = request.POST['city']
                    user.state = request.POST['state']
                    user.country = request.POST['country']
                    user.mobile = request.POST['mobile']

                    user.save()
                    return HttpResponseRedirect('/personal_details/')
                    # return HttpResponseRedirect('%s' % (reverse('personal_details')))
            context = {
                'form': form,
            }
            return render(request, 'login/personal_details_update.html', context)


        def personal_details(request):
        if not request.user.is_authenticated():
            return HttpResponseRedirect('/login/')
        else:
            return render(request, 'login/personal_details.html')



## urls.py ##

        from django.conf.urls import url
        from . import views

        app_name = 'login'

        urlpatterns = [
        url(r'^$', views.home, name='home'),
        url(r'^about us/$', views.about_us, name='about_us'),
        url(r'^services/$', views.services, name='services'),
        url(r'^registration/$', views.registration, name='registration'),
        url(r'^login/$', views.login_user, name='login'),
        url(r'^admin/$', views.admin_login, name='admin_login'),
        url(r'^logout/$', views.logout_user, name='logout'),
        url(r'^personal_details/$', views.personal_details, name='personal_details'),
        url(r'^personal_details/update/$', views.personal_details_update, name='personal_details_update'),
        ]



## forms.py ##

            from django import forms
            from .models import User
            from django.contrib.admin.widgets import AdminDateWidget


        class UserForm(forms.ModelForm):
            date_of_birth = forms.DateField(widget=AdminDateWidget)
            password = forms.CharField(widget=forms.PasswordInput)
            confirm_password = forms.CharField(widget=forms.PasswordInput)

            class Meta:
                model = User
                fields = ['username', 'email', 'first_name', 'last_name', 'date_of_birth', 'address', 'city', 'state',
                          'country', 'mobile', 'avatar', 'password', 'confirm_password']


        class PersonalDetailsForm(forms.ModelForm):
            date_of_birth = forms.DateField(widget=AdminDateWidget)

            class Meta:
                model = User
                fields = ['username', 'email', 'first_name', 'last_name', 'address', 'city', 'state',
                          'country', 'mobile']
                exclude = ['date_of_birth', 'avatar', 'password', 'confirm_password']




## personal_details.html ##

        {% extends 'upload/base.html' %}
        {% block title %}Cloud | Personal Details{% endblock %}
        {% block folders_active %}active{% endblock %}
        {% load staticfiles %}
        {% block navigation%}
        <div class="container-fluid files-container">
            <div class="row">
                <div class="col-sm-12 col-md-4">
                    <div class="panel panel-default" style="background: transparent">
                        <div class="panel-body">
                            <div class="page-header">
                                {% if msg %}
                                    <p><strong><font color="#dc143c">{{ msg }}</font></strong></p>
                                {% endif %}
                            </div>
                            <div class="row">
                                <h4>Personal Details &nbsp;<a href="{% url 'login:personal_details_update' %}" class="btn-success" style="background: transparent"><span class="glyphicon glyphicon-edit"></span></a></h4><br>
                            </div>
                            <form class="form-horizontal" role="form" action="" method="post" enctype="multipart/form-data">
                                <div class="form-group">
                                    <label class="control-label" for="id_username">
                                        Username:
                                    </label>
                                    {{ user.username }}
                                </div>
                                <div class="form-group">
                                    <label class="control-label" for="id_email">
                                        Email:
                                    </label>
                                    {{ user.email }}
                                </div>
                                <div class="form-group">
                                    <label class="control-label" for="id_first_name">
                                        First Name:
                                    </label>
                                    {{ user.first_name }}
                                </div>
                                <div class="form-group">
                                    <label class="control-label" for="id_last_name">
                                        Last Name:
                                    </label>
                                    {{ user.last_name }}
                                </div>
                                <div class="form-group">
                                    <label class="control-label" for="id_date_of_birth">
                                        Date Of Birth:
                                    </label>
                                    {{ user.date_of_birth }}
                                </div>
                                <div class="form-group">
                                    <label class="control-label" for="id_address">
                                        Address:
                                    </label>
                                    {{ user.address }}
                                </div>
                                <div class="form-group">
                                    <label class="control-label" for="id_city">
                                        City:
                                    </label>
                                    {{ user.city }}
                                </div>
                                <div class="form-group">
                                    <label class="control-label" for="id_state">
                                        State:
                                    </label>
                                    {{ user.state }}
                                </div>
                                <div class="form-group">
                                    <label class="control-label" for="id_country">
                                        Country:
                                    </label>
                                    {{ user.country }}
                                </div>
                                <div class="form-group">
                                    <label class="control-label" for="id_mobile">
                                        Mobile:
                                    </label>
                                    {{ user.mobile }}
                                </div>
                                <div class="form-group">
                                    <label class="control-label" for="id_avatar">
                                        Avatar:
                                    </label>
                                    {{ user.avatar }}
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        {% endblock %}



## personal_details_update.html ##

        {% extends 'upload/base.html' %}
        {% block title %}Cloud | Personal Details Update{% endblock %}
        {% block folders_active %}active{% endblock %}
        {% load staticfiles %}
        {% block navigation%}
        <div class="container-fluid files-container">
            <div class="row">
                <div class="col-sm-12 col-md-4">
                    <div class="panel panel-default" style="background: transparent">
                        <div class="panel-body">
                            <div class="page-header">
                                {% if msg %}
                                    <p><strong><font color="#dc143c">{{ msg }}</font></strong></p>
                                {% endif %}
                            </div>
                            <div class="row">
                                <h4>Personal Details Update Form</h4><br>
                            </div>
                            <form class="form-horizontal" role="form" action="" method="post" enctype="multipart/form-data">

                                {% csrf_token %}

                                <div class="form-group">
                                    <label class="control-label" for="id_username">
                                        Username:
                                    </label>
                                    {{ form.username }}
                                </div>

                                <div class="form-group">
                                    <label class="control-label" for="id_email">
                                        Email:
                                    </label>
                                    {{ form.email }}
                                </div>

                                <div class="form-group">
                                    <label class="control-label" for="id_first_name">
                                        First Name:
                                    </label>
                                    {{ form.first_name }}
                                </div>
                                <div class="form-group">
                                    <label class="control-label" for="id_last_name">
                                        Last Name:
                                    </label>
                                    {{ form.last_name }}
                                </div>

                                <div class="form-group">
                                    <label class="control-label" for="id_address">
                                        Address:
                                    </label>
                                    {{ form.address }}
                                </div>

                                <div class="form-group">
                                    <label class="control-label" for="id_city">
                                        City:
                                    </label>
                                    {{ form.city }}
                                </div>

                                <div class="form-group">
                                    <label class="control-label" for="id_state">
                                        State:
                                    </label>
                                    {{ form.state }}
                                </div>

                                <div class="form-group">
                                    <label class="control-label" for="id_country">
                                        Country:
                                    </label>
                                    {{ form.country }}
                                </div>

                                <div class="form-group">
                                    <label class="control-label" for="id_mobile">
                                        Mobile:
                                    </label>
                                    {{ form.mobile }}
                                </div>

                                <div class="form-group">
                                    <div class="col-sm-offset-5">
                                        <button type="submit" class="btn btn-success">Update</button>
                                    </div>
                                </div>

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

This is the error message i'm getting when i try to update my personal_details form. Please someone help me fix it

1 个答案:

答案 0 :(得分:0)

我认为错误是,您指定&#34;用户&#34;变量

user = request.user

而不是这个,你需要这样做,

user = User.objects.get(username=request.user.username)

对于后者,您指的是用户对象,您可以在其上更新或更改数据。

虽然在前一种情况下,django正在尝试创建一个已经存在于数据库中的新用户。这就是为什么它会给出你得到的错误。你只需从数据库中获取一个并对其进行操作。

我也是django的新手,希望你得到你想要的东西。