我已经和Django合作了一段时间,今天我遇到了一个问题,在我的生活中,我无法弄明白。我试图加载他们自己在上一步保存的用户个人资料,但是当我尝试打开个人资料所在的页面时,他们可以看到并编辑它我得到了我提到的错误。
以下是我的views.py
@verified_email_required()
def home(request):
usuario = Perfil.objects.filter(user=request.user)
context = ({"usuario": usuario})
return render(request, "explore/inicioapp.html", context)
@verified_email_required()
def profile(request, id):
instance = get_object_or_404(Perfil, id=id)
form = ProfileForm(instance=instance)
if request.method == "POST":
form = ProfileForm(request.POST, instance=instance)
if form.is_valid():
perfil = form.save(commit=False)
perfil.user = request.user
perfil.save()
return HttpResponseRedirect("/profile/")
context = ({"form", form}, {"datos": instance})
return render(request, "explore/profile.html", context)
models.py
class Perfil(models.Model):
user = models.OneToOneField(User)
Sexo = models.CharField(max_length=100)
Direccion = models.CharField(max_length=100)
CP = models.CharField(max_length=100)
Ciudad = models.CharField(max_length=100)
Estado = models.CharField(max_length=100)
Pais = models.CharField(max_length=100)
Telefono = models.CharField(max_length=100)
Celular = models.CharField(max_length=100)
PaisPasaporte = models.CharField(max_length=100)
NumeroPasaporte = models.CharField(max_length=100)
VigenciaPasaporte = models.DateField(max_length=100)
ContactoEmergencia = models.CharField(max_length=100)
TelefonoEmergencia = models.CharField(max_length=100)
CorreoEmergencia = models.CharField(max_length=100)
Alergias = models.CharField(max_length=500)
forms.py
class ProfileForm(forms.ModelForm):
class Meta:
model = Perfil
exclude = ["user"]
widgets = {
'Sexo': Select(choices=opciones_sexo, attrs={'class': 'selectpicker'}),
'VigenciaPasaporte': forms.DateInput(attrs={'class': 'datepicker'})
}
labels = {
'Sexo': _("Gender"),
'Direccion': _("Address"),
'CP': _("Zip code"),
'Ciudad': _("City"),
'Estado': _("State"),
'Pais': _("Country"),
'Telefono': _("Phone"),
'Celular': _("Cellphone"),
'PaisPasaporte': _("Passport emission country"),
'NumeroPasaporte': _("Passport number"),
'VigenciaPasaporte': _("Passport expiration date"),
'ContactoEmergencia': _("Emergency contact person"),
'TelefonoEmergencia': _("Emergency contact phone"),
'CorreoEmergencia': _("Emergency contact email")
}
def __init__(self, *args, **kwargs):
kwargs.setdefault("label_suffix", "")
super(ProfileForm, self).__init__(*args, **kwargs)
def clean(self):
cleaned_data = super(ProfileForm, self).clean()
sexoseleccionado = cleaned_data.get("Sexo")
if sexoseleccionado == "none":
raise forms.ValidationError("You must select a gender to continue.")
return cleaned_data
URL
url(r'^profile/(?P<user>\d+)$', views.profile, name="profile"),
最后是HTML链接
<a class="btn btn-menu" href="{% url "explore:profile" Perfil.id %}">{% trans "My Profile" %}</a>
谢谢!
答案 0 :(得分:1)
你的问题是你的url模式正在传递一个参数用户,但你的view方法是将参数定义为id。
url(r'^profile/(?P<user>\d+)$', views.profile, name="profile"),
^^^^
您的查看方法:
@verified_email_required()
def profile(request, id):
^^
答案 1 :(得分:0)
这只是一个名称问题。在个人资料的网址中,您正在捕获&#34;用户&#34;变量。但是观点本身正在期待一个&#34; id&#34;论点。使这些一致。