我正在尝试更新UserProfile模型

时间:2016-05-20 13:17:34

标签: django

一切都在代码中工作,但它没有存储数据。甚至用户都被HTTPResponseRedirect重定向,但它没有存储数据。如果有任何其他方式来更新我的UserProfile

Views.py
@login_required
def p1details(request):

    if request.method == 'POST':
        #form=p1detailsform(data=request.POST)
        script = request.POST.get('script')
        request.user.info = script


        request.user.save()
        print("cmon")
        return HttpResponseRedirect('/harpoons/dashboard')

    else:

        return render(request, 'p1details.html', {})
Models.py

class UserProfile(models.Model):
    # This line is required. Links UserProfile to a User model instance.
    user = models.OneToOneField(User)
    GENRE_CHOICES = (
        ('m', 'Masculino'),
        ('f', 'Feminino'),
    )

    # The additional attributes we wish to include.
    website = models.URLField(blank=True,)
    info = models.CharField(max_length=30,blank=True)
    birth_date = models.DateField(null=True)
    genre = models.CharField(max_length=1, choices=GENRE_CHOICES, null=True)
    script = models.TextField(max_length=None, null=True)
    #my_field = tinymce_models.HTMLField(max_length=None, null=True)
    docfile = models.FileField(upload_to='documents/%Y/%m/%d', null=True)

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


Template

<form method="post" action="{% url 'harpoons:p1details' %}">
     {% csrf_token %}
 <input type="text" name="script">kuch bhar
     <input type="submit" name="submit" value="Provide details" />
</form>

2 个答案:

答案 0 :(得分:1)

实际上您应该使用表单来验证数据。在这种情况下:

request.user.userprofile.info = script
request.user.userprofile.save()

答案 1 :(得分:1)

您正在尝试保存用户对象而不是userprofile对象

request.user.userprofile.info = script
request.user.userprofile.save()