【发布时间】:2012-05-14 20:43:12
【问题描述】:
我正在创建一个带有用户的小 Django 应用程序,并且我创建了自己的 UserProfile 模型。但我的网址有一些问题(至少我认为)。我认为我使用的正则表达式是错误的。看看吧:
我得到的错误:
ValueError at /usr/tony/
invalid literal for int() with base 10: 'tony'
我的网址:
url(r'^usr/(?P<username>\w+)/$', 'photocomp.apps.users.views.Userprofile'),
我的看法:
from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
from django.contrib import auth
from django.http import HttpResponseRedirect
from photocomp.apps.users.models import UserProfile
def Userprofile(request, username):
rc = context_instance=RequestContext(request)
u = UserProfile.objects.get(user=username)
return render_to_response("users/UserProfile.html",{'user':u},rc)
这是我的模型:
from django.db import models
from django.contrib.auth.models import User
class UserProfile(models.Model):
user = models.ForeignKey(User, unique=True)
first_name = models.CharField(max_length="30", blank=True)
last_name = models.CharField(max_length="30", blank=True)
email = models.EmailField(blank=True)
country = models.CharField(blank=True,max_length="30")
date_of_birth = models.DateField(null=True)
avatar = models.ImageField(null=True, upload_to="/avatar")
【问题讨论】:
标签: python regex django django-views django-urls