社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Django

django updateview:valueerror:int()的文本无效,基数为10:username

Simran • 5 年前 • 1461 次点击  

我有一个django应用程序,它包含一个基本的概要模型,该模型使用onetoonefield扩展用户模型

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

这是/updateprofile的url模式

path('updateProfile/<pk>', views.UpdateProfileView.as_view(), name='update_profile'),

从网站的导航栏中调用,如下所示:

<a href="{% url 'update_profile' request.user %}">UpdateProfile</a>

我在forms.py中使用了modelform类,如下所示:

class update_profile_form(forms.ModelForm):
   class Meta:
     model = Profile
     fields = ('phone_number', 'profile_picture')

并在views.py中调用以下基于类的updateview视图:

@method_decorator(login_required, 'dispatch')
class UpdateProfileView(UpdateView):
   model = Profile
   success_url = reverse_lazy('home')
   form_class = update_profile_form
   template_name = 'update_profile.html'

   # this is where the error occurs
   def get_queryset(self):
      return Profile.objects.filter(user=self.request.user) 

my urls.py文件包含以下模式:

 urlpatterns = [
   path('', TemplateView.as_view(template_name='Login/home.html'), name='home'),
   path('home/', TemplateView.as_view(template_name='Login/home.html'), name='home'),
   path('updateProfile/<pk>', views.UpdateProfileView.as_view(), name='update_profile'),
 ]

我无法确定在update_profile.html模板中使用上述用户字段的哪种组合来呈现表单。

请帮忙!谢谢

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/44084
 
1461 次点击  
文章 [ 3 ]  |  最新文章 5 年前
Bidhan Majhi
Reply   •   1 楼
Bidhan Majhi    6 年前

试试这个

class UpdateProfileView(LoginRequiredMixin, UpdateView):
    login_url = '/add login link/'
    redirect_field_name = 'add login field'
    model = Profile
    success_url = reverse_lazy('home')
    form_class = update_profile_form
    template_name = 'update_profile.html'

   # add post function if you have

在HTML中,

<a href="{% url 'update_profile' profile.user.pk %}">UpdateProfile</a>

在URL中,

path('updateProfile/<int:pk>/', views.UpdateProfileView.as_view(), name='update_profile'),
shafik Huskell
Reply   •   2 楼
shafik Huskell    6 年前

这是返回一个对象,以便出现问题

return Profile.objects.filter(user=self.request.user)

把这个改成

return Profile.objects.filter(user=self.request.user.pk) 
Exprator
Reply   •   3 楼
Exprator    6 年前

在HTML中更改此行

<a href="{% url 'update_profile' request.user %}">UpdateProfile</a>

<a href="{% url 'update_profile' request.user.id %}">UpdateProfile</a>

作为 request.user returns user object 但是 url pk 它需要一个 int 领域