Redirect after login not working - django

I've been searching around but can't figure out why the redirection after login is not working as expected.
I successfully login at http://localhost:8000/accounts/login/. After that I'm always redirected to http://localhost:8000 no matter if I use settings.py variable LOGIN_REDIRECT_URL or if I leave it completely out.
# urls.py
from django.conf.urls import patterns, include, url
from django.conf import settings
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^grappelli/', include('grappelli.urls')),
url(r'^$', 'myapp.views.home', name='home'),
url(r'^home/$', 'myapp.views.apphome', name='apphome'),
url(r'^admin/', include(admin.site.urls)),
(r'^accounts/', include('registration.backends.default.urls')),
(r'^admin/', include("massadmin.urls")),
)
I don't know where I should start looking for a problem. Any ideas? Is there some settings I'm missing here? Any other files I should include to help debug the problem?

Check your form action! (glad I could help :) )

Related

Display static page in Django

I am trying to display contents of a static page in Django project.
urls.py :-
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'spollow.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
(r'^$', 'django.views.generic.simple.direct_to_template', {'template': 'index.html'}),
url(r'^admin/', include(admin.site.urls)),
)
index.html is in the same directory as urls.py
I am getting 500 internal server error. Any ideas where I am going wrong?
First of all, what is the stacktrace from the 500 error saying that the error may be? You may be using Django 1.6 and the call to direct_to_template is deprecated.
On Django 1.5 or newer you can use TemplateView
Here's the example from the documentation
https://docs.djangoproject.com/en/dev/topics/class-based-views/
from django.conf.urls import patterns
from django.views.generic import TemplateView
urlpatterns = patterns('',
(r'^about/', TemplateView.as_view(template_name="about.html")),
)
You can use the direct_to_template view on Django 1.4 or older
Here's the relevant documentation
https://docs.djangoproject.com/en/1.4/ref/generic-views/#django-views-generic-simple-direct-to-template
from django.views.generic.simple import direct_to_template
urlpatterns = patterns('',
(r'^foo/$', direct_to_template, {'template': 'foo_index.html'}),
(r'^foo/(?P<id>\d+)/$', direct_to_template, {'template': 'foo_detail.html'}),
)
If it is the latter, I would use a module instead of string, (look at the import on the example).
Other than that, without the 500 details it will be shooting in the dark, you may not have the right template, or an incorrect path, or a million different things.
Bonus note
If you just want to serve static pages, it might be better to serve them through the actual webserver in front of django (nginx, apache, etc), specially if you are expecting a high volume of traffic.
If Your error is due to unable to find index.html
if yours is an app(ie: created by python manage.py startapp <app>) then:
Then django will search for template files in <app>/templates directory, if you added the app to INSTALLED_APPS in settings.py.
so you need to create a folder templates inside your <app> and put index.html inside it.
if you don't have any apps, you want to add the template path manually :
open settings.py, then edit TEMPLATE_DIRS
TEMPLATE_DIRS = (
# Put the full path of the template dir here, like "/home/html/django_templates" or
# "C:/www/django/templates".
)
In Django 1.5 or newer you can use the render function instead of direct_to_template:
from django.conf.urls import patterns, include, url
urlpatterns = patterns('',
url(r'^$', 'django.shortcuts.render', {'template_name': 'index.html'}),
)
Or if you prefer the more complex way :), you can use class-based TemplateView:
from django.conf.urls import patterns, include, url
from django.views.generic import TemplateView
urlpatterns = patterns('',
url(r'^$', TemplateView.as_view(template_name="index.html")),
)

How to add more than one link in Django 1.6.1 urls.py

I am trying to add more than one link to my urls.py. First code below is in the app url, and other one in the main urls. However; when I am trying to add REGISTER and run the server, it still displays me the same of BLOG page.
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^blog/', include('blog.urls')),
url(r'^register/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),)
This one in the main urls.py
from django.conf.urls import patterns, url
from blog import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^$', views.register, name='register'),)
What should I do to fix this situation? Any idea will be appreciated. Thanks in advance
Your main urls.py should have:
url(r'^register/$', views.register, name='register'),)
and you should remove the register from app urls.
This will make yourapp.com/register/ point to views.register (or possibly yourapp.com/blog/register/ view it - I'm slightly confused on which urls.py is taking precedence)
The first block of code should be in your project's folder/project folder/urls.py, so paste it there. The second block is perfect, it should be placed in the new urls.py that you have created recently in the apps folder.

In Django, How do you write the url pattern for '/' and other root-based urls

I'm new to django, and one of the things that I'm still learning is url_patterns. I set up a pages app to handle the root path (http://www.mysite.com) as well as some static pages such as the about page. I figured out how to set up the url pattern for the root path, but I can't get the site to direct the path '/about' to the pages "about" view.
Here is my main urls.py
from django.conf.urls import patterns, include, url
from django.conf import settings
urlpatterns = patterns('',
url(r'^polls/', include('polls.urls')),
url(r'^$', 'pages.views.root'),
url(r'^/', include('pages.urls')),
)
here is my pages urls.py
from django.conf.urls import patterns, include, url
urlpatterns = patterns('pages.views',
url(r'^about', 'about'),
)
Here is my pages views.py
# Create your views here.
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.http import HttpResponse, HttpResponseRedirect
from django.core.urlresolvers import reverse
def root(request):
return render_to_response('pages/root.html',context_instance=RequestContext(request))
def about(request):
return render_to_response('pages/about.html',context_instance=RequestContext(request))
If I change the main urls.py file to have r'^a/', include('pages.urls') then the path '/a/about' directs to the about action.. so I think it has to be an issue in the way i'm writing the url pattern in this file. But, I can't figure out how to change it. Can anyone help?
Figured out what the issue is. The proper url_pattern on the project level is:
urlpatterns = patterns('',
url(r'^polls/', include('polls.urls')),
url(r'^$', 'pages.views.root'),
url(r'', include('pages.urls')),
)
When this is in place, '/about' and other simple paths direct properly.
Thanks everyone!
Try this, for url.py on the project level:
urlpatterns = patterns('',
# Examples:
url(r'^$', 'apps_name.views.home', name='home'),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
(r'^about/', include('about.urls')),
)
and then the url.py for the app about
urlpatterns = patterns('',
url(r'^$', direct_to_template, {"template": "about/about.html"}, name="about"),
)
Take into account that regular expression are evaluated from top to bottom, then if the path fits the regexp it will enter. To learn more about regexp google it or try the great book from Zed Shaw about regexps
Note that from Django version 2.0 the URL pattern has changed to use django.urls.path() Check Example here: link
from django.urls import path
from . import views
urlpatterns = [
# ex: /polls/
path('', views.index, name='index'),
# ex: /polls/5/
path('<int:question_id>/', views.detail, name='detail'),
# ex: /polls/5/results/
path('<int:question_id>/results/', views.results, name='results'),
# ex: /polls/5/vote/
path('<int:question_id>/vote/', views.vote, name='vote'),
]
About the url method:
url(r'^$', 'pages.views.root')
url is deprecated in Django 3.1, it's good to use re_path instead.
https://docs.djangoproject.com/en/3.1/ref/urls/#s-url
https://docs.djangoproject.com/en/3.1/ref/urls/#re-path
Note: The r'^$'pattern WON'T work with path function, and will give you a misleading error that the route could not be found.
You have to use re_path(r'^$', [etc]) every time you use a regular expression instead of a simple string in pattern.

django gets a 404 page when I try to access django-registration

Hello and thank you in advance,
When I installed django-profiles and django-registration. I believe I followed a tutorial correctly on how to configure them:
http://agiliq.com/books/djenofdjango/chapter5.html#reusable-apps
Now when I try to access website.com/accounts I get a 404 page:
Using the URLconf defined in acme_p.urls, Django tried these URL patterns, in this order:
^$
^accounts/
^admin/doc/
^admin/
The current URL, accounts, didn't match any of these.
My urls files look like this:
#acme_p urls - Django Project
from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', include('user_prof.urls')),
# Examples:
# url(r'^$', 'acme_p.views.home', name='home'),
# url(r'^acme_p/', include('acme_p.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
)
user_prof urls.py - Django App one level below project mentioned above
from django.conf.urls.defaults import *
urlpatterns = patterns('',
(r'^login/$', 'auth.views.login_user'),
(r'^accounts/', include('registration.urls')),
# (r'^accounts/', include('registration.backends.default.urls')),
)
Thank you for you help.
dp
(r'^accounts/', include('registration.urls')), should be in the root urls.py not the app urls.py
There is no website.com/account in the configuration, there is only website.com/account/(more) where (more) has the rest of the url that is given to the django-registration module. You could try website.com/account/login/ for example to check if the django-registration module is working.
Also you should just put those on the root urls.py file.

Django cannot find admin.sites.url

I am developing a django site locally using rc1.3 and after making several adjustments to my urls.py file i am receiving error messages that django cannot import admin.site.urls. by urls.py file is below.
urlpatterns = patterns('',
# Example:
(r'^city/$', 'venue.views.city_index'),
(r'^accounts/', include('registration.urls')),
(r'^accountss/', include('registration.backends.simple.urls')),
(r'^profiles/', include('profiles.urls')),
(r'^admin/', include('admin.site.urls')),
(r'^city/(?P<city>[-\w]+)/$', 'venue.views.city_detail'),
(r'^city/(?P<city>[-\w]+)/venue/$', 'venue.views.venue_index'),
)
Any idea what might be cause this, thanks.
http://docs.djangoproject.com/en/dev/intro/tutorial02/#activate-the-admin-site
Don't include the string path, actually import the admin module and point to its urls.
from django.contrib import admin
urlpatterns += patterns('',
(r'^admin/', include(admin.site.urls)),
)