Question

I have a stupid problem about a wrong url...

In the html menu:

<a href="/user_app/update_profile?pk={{ user.pk }}">Update my profile</a>

In urls.py:

urlpatterns = patterns('',
    (r'^update_profile', login_required(UpdateProfile.as_view())),
)

In view.py:

class UpdateProfile(UpdateView):
    form_class = UpdateProfileForm
    template_name = "user_app/update_profile.html"

    #add a success message if the form is valid
    def form_valid(self, form):
        messages.success(self.request, "Your profile has been updated.", extra_tags='success')
        return super(UpdateProfile, self).form_valid(form)

    #modifcation: display the user's data given his primary key
    def get_object(self):
        return UserModel.objects.get(pk=self.request.GET.get('pk'))

In my template update_profile.html:

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

Now my problem... From the menu I click on "Update my profile". The url is "/user_app/update_profile?pk=1". The page is displayed, I can change the data. Now I click on submit to save the modifications. I want the program to stay on the page but it does not, it changes to "/user_app/". And django displays the following error:

Using the URLconf defined in arpaso.urls, Django tried these URL patterns, in this order:
^/?$
^auth/?$
^user_app/? ^show_profile
^user_app/? ^update_profile
The current URL, user_app/, didn't match any of these.

Why is my url changed on submit?

Edit: weird error...

If I use a template context processor to get the current url, I can use the full current url in my template:

<form action="{{ current_path }}" method="post">

When I display it, I get current_path="/user_app/update_profile?pk=1"

Now, when I submit the form, I am redirected to "/users/admin/" and get an error (I have no such url in my website). Where does this url come from?

Was it helpful?

Solution

EDIT: SOLVED!

I just added get_success_url to my view and now it works :):

def get_success_url(self):
    succ_url =  '/user_app/update_profile?pk='+self.request.GET.get('pk')
    return succ_url
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top