你能帮我解决Django和自定义用户模型吗?我是Django世界的新手,所以我开始学习最新的1.7版本。在我的项目中,我正在使用django-allauth包,我想在contrib.auth的标准用户类和我的自定义模型“users”之间创建OneToOne关系。
#models.py
import datetime
from django.db import models
from django.contrib.auth.models import User
class Profile(models.Model):
user=models.OneToOneField(User)
f_name=models.CharField(null=True, blank=True, max_length='30')
l_name=models.CharField(null=True, blank=True, max_length='30')
birth_date=models.DateField(null=True, blank=True)
company=models.CharField(null=True, blank=True, max_length='30')
rate=models.FloatField(null=True, blank=True, default=0.0)
skills=models.CharField(null=True, blank=True, max_length='255')
bill_rate=models.CharField(null=True, blank=True, max_length='255')
contacts=models.CharField(null=True, blank=True, max_length='255')
portfolio=models.CharField(null=True, blank=True, max_length='127')
avatar = models.ImageField(upload_to='/static/img/%Y/%m/%d', blank=True, null=True)
def __unicode__(self):
return self.user.username
这是forms.py
#forms.py
from django.forms import ModelForm
from users.models import Profile
class ProfileForm(ModelForm):
class Meta:
model = Profile
fields = ('f_name', 'l_name', 'company', )
这是一个模板:
{% extends "base_.html" %}
{% block main_content%}
<div>
{% if user.is_authenticated %}
<p>Welcome, {{ request.user.id }}!</p>
<form id="profile_form" method="POST" action="/accounts/profile" enctype="multipart/form-data">
{% csrf_token %}
{{ profile_form.as_p }}
<input type="submit" name="submit" value="Save" />
</form>
{% endif %}
</div>
{% endblock%}
和views.py:
from django.http import HttpResponse
from django.template import RequestContext
from django.conf import settings
from django.shortcuts import render, render_to_response
from models import Profile
from forms import ProfileForm
def edit(request):
profile_form = ProfileForm()
user = request.user
if request.method == 'POST':
profile_form = ProfileForm(data=request.POST, instance=user)
if profile_form.is_valid():
profile = profile_form.save(commit=False)
profile.user = user
profile.save()
return render_to_response("profile.html", RequestContext(request))
return render_to_response('profile_edit.html', RequestContext(request, {'profile_form' : profile_form}))
我根据本教程构建了所有这些文件: http://www.tangowithdjango.com/book/chapters/login.html
最后,所有这一切都很好,但是当我按下Save按钮时,POST表单不起作用。我看不到数据库的任何变化。重定向工作很好,实际上似乎一切正常,但我表中没有任何数据(顺便说一句,我使用的是postgres)。
我花了很多时间来找到答案并尝试了互联网上的几乎所有内容,但我仍然有这个问题。也许我无法从django文档中了解它是如何工作的,但我尝试了相同的方法并且仍然存在问题。
提前谢谢!
更新 我的urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
# Examples:
url(r'^$', 'laniakea.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^accounts/', include('allauth.urls')),
url('accounts/profile', 'laniakea.views.prof', name='prof'),
url(r'^edit', 'users.views.edit', name='edit'),
)
答案 0 :(得分:0)
我找到了解决方案!正如丹尼尔所说,我改变了行动&#34;用于将POST请求传输到同一表单的属性。正如我发现的那样,我没有创建一个正确的User父类实例。实际上,我不需要使用它们。
#views.py
from django.template import RequestContext
from django.shortcuts import render
from models import Profile
from forms import ProfileForm
def edit(request):
user = request.user
if request.method == 'POST':
profile_form = ProfileForm(request.POST)
if profile_form.is_valid():
profile = profile_form.save(commit=False)
profile.user = user
profile.save()
return HttpResponseRedirect('accounts/profile')
else:
profile_form = ProfileForm()
return render(request, 'profile_edit.html', {'profile_form' : profile_form})
和
#profile_edit.html
{% extends "base_.html" %}
{% block main_content%}
<div>
{% if user.is_authenticated %}
<p>Welcome, {{ request.user.id }}!</p>
<form id="profile_form" method="POST" action="" enctype="multipart/form-data">
{% csrf_token %}
{{ profile_form.as_p }}
<input type="submit" name="submit" value="Save" />
</form>
{% endif %}
</div>
{% endblock%}
非常感谢您的帮助和祝你好运!