[Fixed]-UpdateView and linking to a user

1πŸ‘

βœ…

I think it’s a bad idea to use the email address as primary keys. What if a user changes their email address?

It might be a better idea to have unique=True for the email address, and let Django create the automatic primary key. Then including (?P<pk>\d+)/ in your url should work.

If you must use the email as the primary key, you need to change the regex from (?P<pk>\d+)/, which will only match digits, to something like

(?P<pk>[\w@.-]+)

The above might not catch all email addresses. I think Django contains a better character class, but I can’t find it at the moment.

πŸ‘€Alasdair

0πŸ‘

I think you are just doing it in a wrong way.
Here is what you need to do:

First of all I’d recommend you to add a pk to your model, as having email as a pk is a bad idea.

  1. Create a url for update view

An example:

url(r'^profile/(?P<pk>[\d]+)/edit/$',
        views.ProfileUpdateView.as_view(), name='edit_profile')
  1. Create an UpdateView

An example:

class ProfileUpdateView(UpdateView):
    model = Freelancer
    fields = ['email']
    template_name = "profiles/profile_edit.html"
  1. Create a template for the form

An example:

<form action="" method="post">{% csrf_token %}
    {{ form.as_p }}
    <input type="submit" value="Update" />
</form>

Now you need to add this link wherever you want:

<a href="{% url 'edit_profile' profile.pk %}">Edit</a>

When you vist the url you will go to update page.

πŸ‘€pythad

Leave a comment