如何在没有配置文件创建视图的情况下创建配置文件更新视图?

时间:2020-11-04 01:07:38

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

在这里,我尝试创建配置文件更新视图,但我没有在views.py中使用配置文件创建视图,因为我是通过信号创建配置文件的,所以,我不需要配置文件创建视图,但是我需要更新视图,以便用户可以更新他的个人资料,我在/ profiles / profiles / admin / 1 /处收到错误 ValueError 视图profile.views.ProfileUpdateView没有返回HttpResponse对象。 我不知道要修复它,这是我的代码,如果您认为我的工作方式很不专业,请告诉我并告诉我更专业的方法。 >

views.py

class ProfileUpdateView(UpdateView):
    refirect_field_name ='profiles:final_detail'
    form_class = UserUpdateForm
    model = UserProfile

    def get_context_data(self, *args, **kwargs):
        context = super(ProfileUpdateView, self).get_context_data(*args, **kwargs)
        update_form = UserUpdateForm(instance = self.request.user)
        context['form']=update_form
        return context

    def form_valid(self,form):
            form.save()  

urls.py

app_name = 'profiles'

urlpatterns = [
    path('final/<str:username>/',FinalProfileDetailView.as_view(),name = 'final_detail'),
    path('profiles/<str:username>/<int:pk>/',ProfileUpdateView.as_view(),name = 'update'),
]

model.py

class UserProfile(models.Model):

    user = models.OneToOneField(User, on_delete=models.CASCADE)
    follower = models.ManyToManyField(User, related_name ='is_following',blank=True,)
    close_friends = models.ManyToManyField(User,related_name='my_close_friends', blank=True)
    avatar = models.ImageField(("Avatar"), upload_to='displays', default = '1.jpg',height_field=None, width_field=None, max_length=None,blank = True)
    background =models.ImageField(("Background"), upload_to='backgrounds', default = 'ak.jpg',height_field=None, width_field=None, max_length=None,blank = True)
    create_date = models.DateField(auto_now_add=True,null=True)
    
   
    objects = ProfileManager()

    def __str__(self):
        return f'{self.user.username}'
    
    def save(self,*args, **kwargs):
        super(UserProfile,self).save(*args, **kwargs) #it will take data and save it

        dp = Image.open(self.avatar.path) #storing avatar in varible
        if dp.height >300 or dp.width >300:
            output_size =(300,300) #set anysize you want
            dp.thumbnail(output_size) 
            dp.save(self.avatar.path) #after resing it save it in data base in place of uploaded once by user

    def get_absolute_url(self):
        return reverse("userprofile_detail", kwargs={"pk": self.pk})

form.py

class UserUpdateForm(forms.ModelForm):
    email = forms.EmailField(required=False)
    class Meta:
        model = get_user_model()
        fields = ["username","email"]

user_update.html

<form method="post">{% csrf_token %}
{{form.as_p}}
<button type="submit" >save</button>
</form>

如果需要更多的代码或信息,而不是在评论会话中告诉我,我将使用该信息更新我的问题。谢谢!

0 个答案:

没有答案