正在获取的用户对象表单.py

2024-04-24 10:53:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个名为profiles的应用程序,它利用了django registration redux模块。我正在尝试创建一个表单,允许用户使用其中已有的信息对其进行编辑,但它没有显示出来。我可以让它在没有信息的情况下显示出来,但不能显示已经存在的配置文件信息。你知道吗

网址.py

from django.conf.urls import url
from profiles import views

urlpatterns = [
    url(r'^(?P<username>[-\w]+)/$', views.single, name='profile_single'),
    url(r'^(?P<username>[-\w]+)/edit/$', views.edit, name="profile_edit"),
] 

视图.py

def edit(request, username):
    instance = Profile.objects.get(user=username)
    # It would be good to have an in depth understanding of what the actual request module does
    if request.user == instance.user:
        form = ProductForm(request.POST or None, instance = instance)

    if form.is_valid():
        profile = form.save(commit=False)
        profile.user = request.user
        profile.slug = slugify(form.cleaned_data['title'])
        profile.save()
        return HttpResponseRedirect('/user/%s'%(user.username))
    return render_to_response("profiles/edit.html", locals(), context_instance=RequestContext(request))

我收到的错误是:

Exception Value: invalid literal for int() with base 10: 'admin'


Tags: djangoinstancefrompyimportform信息url
1条回答
网友
1楼 · 发布于 2024-04-24 10:53:48

您需要检查username模型的User字段。你知道吗

为此,请替换以下行:

instance = Profile.objects.get(user=username)

使用:

instance = Profile.objects.get(user__username=username)

相关问题 更多 >