gpt4 book ai didi

Django UserProfile匹配查询不存在

转载 作者:行者123 更新时间:2023-12-01 11:00:04 25 4
gpt4 key购买 nike

我正在使用本教程创建一个论坛 http://lightbird.net/dbe/forum2.html我遇到了一个错误。

当我点击 edit profile 时,它​​应该指向这个页面而不是我得到一个错误(如下)

enter image description here

 DoesNotExist at /forum/profile/1/

UserProfile matching query does not exist.

Request Method: GET
Request URL: http://127.0.0.1:8000/forum/profile/1/
Django Version: 1.4.3
Exception Type: DoesNotExist
Exception Value:

UserProfile matching query does not exist.

Traceback Switch to copy-and-paste view

C:\djcode\mysite\forum\views.py in profile

profile = UserProfile.objects.get(user=pk)

我认为这个错误意味着,django 无法接收我的管理员登录。

这是我的 fbase.html 的一部分

  <a href="{% url ben:profile user.pk %}">Edit profile</a>

指向我的 URLconf。

  from django.conf.urls import patterns,include,url
from django.contrib import admin
from django.conf import settings

urlpatterns = patterns('forum.views',
url(r'^$','main',name='main'),
url(r"^forum/(\d+)/$", "forum",name ="forum"),
url(r"^thread/(\d+)/$","thread",name = "thread"),
url(r"^post/(new_thread|reply)/(\d+)/$", "post",name = "post"),
url(r"^reply/(\d+)/$", "reply" , name ="reply"),
url(r"^new_thread/(\d+)/$", "new_thread" , name ="new_thread"),
url(r"^profile/(\d+)/$", "profile",name= "profile"),
)

进入我的 View 函数

  def profile(request, pk):
"""Edit user profile."""
profile = UserProfile.objects.get(user=pk)
img = None

if request.method == "POST":
pf = ProfileForm(request.POST, request.FILES, instance=profile)
if pf.is_valid():
pf.save()
# resize and save image under same filename
imfn = pjoin(MEDIA_ROOT, profile.avatar.name)
im = PImage.open(imfn)
im.thumbnail((160,160), PImage.ANTIALIAS)
im.save(imfn, "JPEG")
else:
pf = ProfileForm(instance=profile)

if profile.avatar:
img = "/media/" + profile.avatar.name
return render_to_response("forum/profile.html", add_csrf(request, pf=pf, img=img))

这是我的模型.py

  from django.db import models
from django.contrib.auth.models import User
from django.contrib import admin
from string import join
from mysite.settings import MEDIA_ROOT

class Forum(models.Model):
title = models.CharField(max_length=60)
def __unicode__(self):
return self.title
def num_posts(self):
return sum([t.num_posts() for t in self.thread_set.all()])

def last_post(self):
if self.thread_set.count():
last = None
for t in self.thread_set.all():
l = t.last_post()
if l:
if not last: last = l
elif l.created > last.created: last = l
return last
class Thread(models.Model):
title = models.CharField(max_length=60)
created = models.DateTimeField(auto_now_add=True)
creator = models.ForeignKey(User, blank=True, null=True)
forum = models.ForeignKey(Forum)

def __unicode__(self):
return unicode(self.creator) + " - " + self.title
def num_posts(self):
return self.post_set.count()

def num_replies(self):
return self.post_set.count() - 1

def last_post(self):
if self.post_set.count():
return self.post_set.order_by("created")[0]
class Post(models.Model):
title = models.CharField(max_length=60)
created = models.DateTimeField(auto_now_add=True)
creator = models.ForeignKey(User, blank=True, null=True)
thread = models.ForeignKey(Thread)
body = models.TextField(max_length=10000)

def __unicode__(self):
return u"%s - %s - %s" % (self.creator, self.thread, self.title)

def short(self):
return u"%s - %s\n%s" % (self.creator, self.title, self.created.strftime("%b %d, %I:%M %p"))
short.allow_tags = True

### Admin

class ForumAdmin(admin.ModelAdmin):
pass

class ThreadAdmin(admin.ModelAdmin):
list_display = ["title", "forum", "creator", "created"]
list_filter = ["forum", "creator"]

class PostAdmin(admin.ModelAdmin):
search_fields = ["title", "creator"]
list_display = ["title", "thread", "creator", "created"]
class UserProfile(models.Model):
avatar = models.ImageField("Profile Pic", upload_to="images/", blank=True, null=True)
posts = models.IntegerField(default=0)
user = models.ForeignKey(User, unique=True)

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

最佳答案

你忘了把 pk 放在你的渲染中:

return render_to_response("forum/profile.html", add_csrf(request, pf=pf, 'profile': profile, img=img))

在 html 中:

<a href="{% url ben:profile profile.id %}">Edit profile</a>

关于Django UserProfile匹配查询不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15048535/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com