Repository: django-wiki/django-wiki

Issue:
404 on initital user create with /wiki
Python Version: 3.5.2
Django version: 1.10
Wiki version: 0.2.4

Wiki URL's are configured to direct to /wiki as indicated in the installation instructions.

When a user goes to the wiki and creates an account, they are able to successfully, at which point they are directed to the login page. On that initial login, they are sent to http://foo.net/accounts/profile/

This results in a 404 error. Django debug giving:

```
Using the URLconf defined in foo.urls, Django tried these URL patterns, in this order:
^$ [name='index']
^admin/
^wiki/
^notifications/
The current URL, accounts/profile/, didn't match any of these.
```

If they return to http://foo.net/wiki they are logged in and are able to log out/in normally without trouble.


Discussion:
You should set this in your project's settings:

```
from django.core.urlresolvers import reverse_lazy
LOGIN_REDIRECT_URL = reverse_lazy('wiki:get', kwargs={'path': ''})
```
In the settings.py file for the whole project? I did that and it gives the same error...or is it supposed to be somewhere else?
Your django project is some folder that has a "manage.py" file and then it has a settings.py file, yes. You can read about it here: https://docs.djangoproject.com/en/1.11/ref/settings/#login-redirect-url
Ah, nevermind, had to restart apache and then it took effect. Works like a charm. Thank you!
Glad you found the solution!

As with any other repository on Github please read [the guidelines for contributing](https://github.com/django-wiki/django-wiki/blob/master/CONTRIBUTING.md). Thank you for your understanding and please help spread the word and stick around the support community (IRC/mailing lists) if you want to help others or have others help you.

![image](https://user-images.githubusercontent.com/374612/27585192-7b055c6c-5b3b-11e7-94a3-aab372de32a4.png)

It seems the same issue occurs when using the wiki to change a user password. It attempts to go to foo.com/_accounts/settings/

It does successfully update the password, it's just when it attempts to reload the page it is trying to direct to the root page rather then to the wiki page.

Resolving patch:
diff --git a/src/wiki/views/accounts.py b/src/wiki/views/accounts.py
--- a/src/wiki/views/accounts.py
+++ b/src/wiki/views/accounts.py
@@ -117,15 +117,33 @@ class Update(UpdateView):
     model = User
     form_class = forms.UserUpdateForm
     template_name = "wiki/accounts/account_settings.html"
-    success_url = "/_accounts/settings/"
 
     def get_object(self, queryset=None):
         return get_object_or_404(self.model, pk=self.request.user.pk)
 
+    def get(self, request, *args, **kwargs):
+        """
+        Save the initial referer
+        """
+        self.referer = request.META.get('HTTP_REFERER', '')
+        request.session['login_referer'] = self.referer
+        return UpdateView.get(self, request, *args, **kwargs)
+
+    def post(self, request, *args, **kwargs):
+        self.referer = request.session.get('login_referer', '')
+        return UpdateView.post(self, request, *args, **kwargs)
+
     def form_valid(self, form):
         pw = form.cleaned_data["password1"]
         if pw is not "":
             self.object.set_password(pw)
-            self.object.save()
+        self.object.save()
+
         messages.info(self.request, _("Account info saved!"))
-        return super(Update, self).form_valid(form)
+
+        # Redirect after saving
+        if self.referer:
+            return redirect(self.referer)
+        if django_settings.LOGIN_REDIRECT_URL:
+            return redirect(django_settings.LOGIN_REDIRECT_URL)
+        return redirect("wiki:root")


Test patch:
diff --git a/tests/core/test_accounts.py b/tests/core/test_accounts.py
new file mode 100644
--- /dev/null
+++ b/tests/core/test_accounts.py
@@ -0,0 +1,64 @@
+from __future__ import absolute_import, print_function, unicode_literals
+
+
+from django.conf import settings as django_settings
+from django.contrib.auth import authenticate
+from django.shortcuts import resolve_url
+
+from ..base import (ArticleWebTestUtils, DjangoClientTestBase,
+                    RequireRootArticleMixin)
+
+
+class AccountUpdateTest(RequireRootArticleMixin, ArticleWebTestUtils, DjangoClientTestBase):
+
+    def test_password_change(self):
+        """
+        Test that we can make a successful password change via the update form
+        """
+        c = self.c
+
+        # Check out that it works as expected, notice that there is no referrer
+        # on this GET request.
+        c.get(
+            resolve_url('wiki:profile_update',)
+        )
+
+        # Now check that we don't succeed with unmatching passwords
+        example_data = {
+            'password1': 'abcdef',
+            'password2': 'abcdef123',
+            'email': self.superuser1.email,
+        }
+
+        # save a new revision
+        response = c.post(
+            resolve_url('wiki:profile_update'),
+            example_data
+        )
+        self.assertContains(response, "Passwords don&#39;t match", status_code=200)
+
+        # Now check that we don't succeed with unmatching passwords
+        example_data = {
+            'password1': 'abcdef',
+            'password2': 'abcdef',
+            'email': self.superuser1.email,
+        }
+
+        # save a new revision
+        response = c.post(
+            resolve_url('wiki:profile_update'),
+            example_data
+        )
+
+        # Need to force str() because of:
+        # TypeError: coercing to Unicode: need string or buffer, __proxy__
+        # found
+        self.assertRedirects(response, str(django_settings.LOGIN_REDIRECT_URL))
+
+        self.assertEqual(
+            self.superuser1,
+            authenticate(
+                username=self.superuser1.username,
+                password=example_data['password1']
+            )
+        )
diff --git a/tests/settings.py b/tests/settings.py
--- a/tests/settings.py
+++ b/tests/settings.py
@@ -1,5 +1,8 @@
 import os
 
+from django.core.urlresolvers import reverse_lazy
+
+
 TESTS_DATA_ROOT = os.path.dirname(__file__)
 
 MEDIA_ROOT = os.path.join(TESTS_DATA_ROOT, 'media')
@@ -63,3 +66,5 @@
         },
     },
 ]
+
+LOGIN_REDIRECT_URL = reverse_lazy('wiki:get', kwargs={'path': ''})