Django - 用户,UserProfile和Admin [英] Django - User, UserProfile, and Admin
问题描述
我正在尝试让Django Admin界面显示有关我个人资料的信息。它显示我的所有用户,但没有配置文件信息。我不太清楚如何让它工作。
我在快速搜索google搜索后发现了这个代码:
来自auth.models的UserProfile
从django.contrib导入admin
从django.contrib.auth.models导入用户
从django.contrib .auth.admin import UserAdmin
admin.site.unregister(User)
class UserProfileInline(admin.StackedInline):
model = UserProfile
class UserProfileAdmin(UserAdmin):
inlines = [UserProfileInline]
admin.site.register(User,UserProfileAdmin)
但是,我不认为它有效。登录管理页面时,我看到用户,组和站点。我点击用户,我看到我的所有用户的列表,但没有任何配置文件的指示。点击一个用户可以显示我有关该用户的信息,但仍然没有个人资料信息。
如果有帮助,这里是我的模型声明:
$ b $从django.db导入模型从django.contrib.auth.models import b
$ user
$ b class UserProfile(models。模型):
company = models.CharField(max_length = 30)
user = models.ForeignKey(User,unique = True)
我的注册码:
def注册(请求):
如果request.method =='POST':
uf = UserForm(request.POST)
upf = UserProfileForm(request.POST)
如果uf.is_valid()和upf.is_valid( )
user = uf.save()
userprofile = upf.save(commit = False)#need以获取用户配置文件对象首先
userprofile.user = user #then设置用户给用户
userprofile.save()#then保存到数据库
return HttpResponseRedirect( '/ auth / login /')
else:
uf = UserForm()
upf = UserProfileForm()
返回render_to_response('register.html',dict(userform = uf ,userprofileform = upf),context_instance = RequestContext(request))
p>我看不清楚是什么错了,但这里有一个稍微简单一些的例子,我知道作品。这是任何工作的admin.py。尝试添加一个逗号到你的内联 - 有些事情没有发生。
从django.contrib import admin
从django.contrib.auth.models导入用户
from django.contrib.auth.admin import UserAdmin
from accounts.models import UserProfile
admin.site.unregister(User)
class UserProfileInline(admin.StackedInline):
model = UserProfile
class UserProfileAdmin(UserAdmin):
inlines = [UserProfileInline,]
admin.site.register(User,UserProfileAdmin)
I'm trying to get the Django Admin interface to display information about my profile. It displays all of my users but no profile information. I'm not quite sure how to get it to work.
I found this code after a quick google search:
from auth.models import UserProfile
from django.contrib import admin
from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin
admin.site.unregister(User)
class UserProfileInline(admin.StackedInline):
model = UserProfile
class UserProfileAdmin(UserAdmin):
inlines = [UserProfileInline]
admin.site.register(User, UserProfileAdmin)
However, I don't think that it worked. When I log into the admin page, I see Users, Groups, and Sites. I click Users and I see a list of all of my Users, but no indication of any profile. Clicking on a user shows me info about that user, but still no profile information.
If it will help, here is my model declaration:
from django.db import models
from django.contrib.auth.models import User
class UserProfile(models.Model):
company = models.CharField(max_length=30)
user = models.ForeignKey(User, unique=True)
And my registration code:
def register(request):
if request.method == 'POST':
uf = UserForm(request.POST)
upf = UserProfileForm(request.POST)
if uf.is_valid() and upf.is_valid():
user = uf.save()
userprofile = upf.save(commit=False)#need to get the user profile object first
userprofile.user = user #then set the user to user
userprofile.save() #then save to the database
return HttpResponseRedirect('/auth/login/')
else:
uf = UserForm()
upf = UserProfileForm()
return render_to_response('register.html', dict(userform=uf,userprofileform=upf),context_instance=RequestContext(request))
I can't see exactly what's wrong, but here's a slightly simpler example that I know works. Put this is any working admin.py. Try adding a trailing comma to your inline-- some things break without it.
from django.contrib import admin
from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin
from accounts.models import UserProfile
admin.site.unregister(User)
class UserProfileInline(admin.StackedInline):
model = UserProfile
class UserProfileAdmin(UserAdmin):
inlines = [ UserProfileInline, ]
admin.site.register(User, UserProfileAdmin)
这篇关于Django - 用户,UserProfile和Admin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!