简体   繁体   English

Django更新视图将无法保存

[英]Django update view won't save

I have this update view but it will not save upon form submission. 我有此更新视图,但提交表单后将无法保存。 5 minutes ago everything was working fine and then I added the edit feature for user posts and all of a sudden nothing will save when trying to edit things. 5分钟前,一切工作正常,然后我为用户帖子添加了编辑功能,突然间,尝试进行任何编辑时都将一无所获。

users app views: 用户应用视图:

class UserEditProfileView(LoginRequiredMixin,UpdateView):
    login_url = '/login/'
    model = UserProfile
    fields = [
            'first_name',
            'profile_pic',
            'location',
            'title',
            'user_type',
            'website',
            'about',
            'twitter',
            'dribbble',
            'github'
            ]
    template_name_suffix = '_edit_form'

    def get_success_url(self):
        userid = self.kwargs['pk']
        return reverse_lazy('users:user_profile',kwargs={'pk': userid})

users app models: 用户应用模型:

class UserProfile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)

    first_name = models.CharField(max_length=50,default='User')
    join_date = models.DateTimeField(default=timezone.now)
    profile_pic = models.ImageField(upload_to='profile_pics',null=True,blank=True)
    location = models.CharField(max_length=150)
    title = models.CharField(max_length=250)
    user_type = models.IntegerField(choices=USER_TYPE_CHOICES,default=1)
    website = models.URLField(max_length=100,blank=True)
    about = models.TextField(max_length=500,default='about')
    twitter = models.CharField(max_length=50,blank=True)
    dribbble = models.CharField(max_length=50,blank=True)
    github = models.CharField(max_length=50,blank=True)

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

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

    def __str__(self):
        return self.user.username

user profile_edit_form.html: 用户profile_edit_form.html:

{% extends "users/base.html" %}

{% block content %}

    <div class="form-title">
        <h2 class="form-title-text">Edit Profile</h2>
    </div>

    <div class="user-forms-base">
        <form method="POST">
            {% csrf_token %}
            {{ form.as_p }}
            <input type="submit" value="Save" />
        </form>
</div>

{% endblock %}

I am having the same issue with updating posts on the home page, however I am assuming the issues are the same and so I'll be able to just replicate this solution over there. 我在更新首页上的帖子时遇到相同的问题,但是我假设这些问题是相同的,因此我将能够在此复制该解决方案。

Someone mentioned this in the comments but basically the form was returning in valid so I override form_invalid to print any errors that may have been causing it. 有人在注释中提到了这一点,但是基本上表单返回的是有效表单,因此我覆盖了form_invalid来打印可能导致它的任何错误。 This showed that it was sending a string when it was expecting an int at the model level. 这表明在模型级别期望int时,它正在发送string Once I switched it back to send an int the problem went away and now it works. 一旦我将其切换回发送int ,问题就消失了,现在可以解决了。 Thanks guys. 多谢你们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM