Reputation: 41
I've been working with Django for a short while and today I ran into a problem that for the life of me I can't figure out. I'm trying to load the user's profile which they themselves have saved on a previous step, however when I try to open the page where the profile should be so they can see it and edit it I am getting the error I mentioned.
Here are my 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"),
And finally the HTML link
<a class="btn btn-menu" href="{% url "explore:profile" Perfil.id %}">{% trans "My Profile" %}</a>
Thanks!
Upvotes: 3
Views: 2563
Reputation: 174698
Your problem is that your url pattern is passing an argument user, but your view method is defining the argument as id.
url(r'^profile/(?P<user>\d+)$', views.profile, name="profile"),
^^^^
Your view method, however:
@verified_email_required()
def profile(request, id):
^^
Upvotes: 2
Reputation: 599936
It's just a name issue. In the URL for profile you are capturing a "user" variable. But the view itself is expecting an "id" argument. Make these consistent.
Upvotes: 0