Django模型表单数据未保存 [英] Django model form data not being saved

查看:110
本文介绍了Django模型表单数据未保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于用户的个人资料模型,其中包含诸如"bio"和"cover"之类的字段,代表有关该用户个人资料的常规信息等.我希望用户可以编辑这两件事.我做了所有的工作,从urls.py到forms.py到views.py到template等,它看起来像我可以提交数据,看起来像是在验证数据,但数据没有保存..我不知道如何..

I have a profile model for the User which has fields like "bio" and "cover" representing the General information about the profile of the user etc.. I want those two things to be able to be edited by the User. I did everything from urls.py to forms.py to views.py to template etc and it looks like I can submit the data and it looks like its validating but the data isnt being saved.. i dont know how..

这是我的文件,我所包含的代码比必要的更多,只是为了查看其他地方的代码是否有错误.

here are my files, i've included more code than neeeded just to see if maybe the code somewhere else is in fault.

请注意,无效的代码是名称中包含通用"字样的代码

note, the code that is not working is the one with "general" somewhere included in the name

urls.py

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

app_name = 'profili'

urlpatterns = [
    #profile
    url(r'^$', views.ProfilePage, name='profile'),
    url(r'^edit/$', views.EditProfile, name='edit_profile'),
    url(r'^edit/general/$', views.EditGeneralProfile, name='edit_general_profile'),
    url(r'^edit/password$', views.EditPassword, name='edit_password'),
    url(r'^edit/delete/$', views.DeleteProfile, name='delete_profile'),
]

views.py

def EditProfile(request):

    if request.method == 'POST':
        form = EditProfileForm(request.POST, request.FILES, instance=request.user)
        if form.is_valid():
            form.save()
            return redirect('/profile')


    else:
        form = EditProfileForm(instance=request.user)
        formpw = PasswordChangeForm(user=request.user)
        generalform = EditGeneralProfileForm(instance=request.user)
        args = {
            'form': form,
            'formpw': formpw,
            'generalform': generalform,
        }
        return render(request, 'profili/editprofile.html', args)



@login_required
def EditGeneralProfile(request):
    generalform = EditGeneralProfileForm(request.POST, request.FILES, instance=request.user)
    if generalform.is_valid():
        generalform.save()
        return redirect('/profile')
    else:
        print('THIS IS NOT WOOORRKIINNGGG')

models.py

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    followall = models.ManyToManyField(User, related_name='followfriend')
    bio = models.TextField(max_length=100, default='', blank=True)
    cover = models.FileField(blank=True)

@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
    if created:
        Profile.objects.create(user=instance)

@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
    instance.profile.save()

forms.py

class EditGeneralProfileForm(forms.ModelForm):

    class Meta:
        model = Profile
        fields = ('bio', 'cover')

模板在GET请求中实际出现的模板

 <form class="form-self general-form" method="post" action="{% url 'profili:edit_general_profile' %}" enctype="multipart/form-data">

          {% csrf_token %}
          {% for item in generalform %}

          <div class="full-part">
            <div class="label-par">
              <span class="label-part">{{ item.label }}:</span>
            </div>
            <div class="original-part">{{ item }}</div>
          </div>

          {% endfor %}

          <div class="full-part-btn">
            <button type="submit" class="fpartbtn">Save</button>
          </div>

        </form>

推荐答案

这是Profile模型的一种形式,因此您应该将其作为实例传递.

This is a form for the Profile model, so you should pass that as the instance.

generalform = EditGeneralProfileForm(request.POST, request.FILES, instance=request.user.profile)

这篇关于Django模型表单数据未保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆