Django keeps showing loggin form after being logged in - django

In a Django project I am using the standard authentication system. The user is redirected to /bewerken/dashboard/, which has two links. One of them is linked to /bewerken/inhoud/. However, when clicking the link, the login form is shown again (at url /bewerken/inhoud/). So, the user logs in and is returnd to /bewerken/dashboard again (because that is the 'next' url). And on and on. Don't know what I'm doing wrong!
The urls.py from the project:
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^bewerken/', include('zzpwebsites.apps.user.urls')),
)
The urls.py from the app:
urlpatterns = patterns('',
url(r'^inhoud/$', views.bewerken_inhoud, name='bewerken_inhoud'),
url(r'^dashboard/$', views.dashboard, name='dashboard'),
url(r'^', 'django.contrib.auth.views.login', {'template_name': 'user/inloggen.html'}),
)
And the view:
#login_required
def bewerken_inhoud(request):
Usp_1 = Usp.objects.filter(user=request.user)
context = ({'usp_1': usp_1,})
return render(request, 'user/bewerken_inhoud.html' , context)
Thanks!

Related

Make arbitrary url as homepage

I have such a urls.py configuration in project repository "forum"
# Project url
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r"^$", views.index, name="index"),
url(r'^article/', include('article.urls',namespace='article')),
]
and with article/urls.py as
#Article app's url config
urlpatterns = [
# show the article list
url(r"^list/(?P<block_id>\d+)$", views.article_list, name="article_list"),
I attempt to take "^article/list/1$" as home page instead of "views.index".
How to make it redirect to "^article/list/1$" when I issue request "127.0.0.1:8000"?
You can redirect from your views.index. In your views.py
from django.shortcuts import redirect
def index(request):
return redirect('article:article_list')
which should take you to article/list. You can include that block_id parameter in the redirect function.
try this
redirect(reverse('article:article_list', kwargs={'block_id':2}))
and make sure to add kwargs in function like this
article_list(request,**kwargs):
I assume you want to this to debug your article page.
You have two ways of doing this.
Either redirect your index view to article view
views.py
from django.shortcuts import redirect
def index(request):
#We force a redirect to article_list view, and we pass arguments.
#Notice it has to come first.
redirect ("article_list", article_list_argument_view=1)
#it will redirect to localhost:8000/article/list/1
'''
Your Home page code.
'''
Put the url directly before the include
urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r"^$", views.index, name="index"),
#The order is important.
url(r'^article/list/1$', "article_list", {'article_list_argument_view':1})
url(r'^article/', include('article.urls',namespace='article')),
]

Django can't find redirect url when authenticating user

I am trying to login protect some of my pages, including my dashboard. Here is the view for my dashboard at the root of my site: sitename.com/
#login_required
def index(request):
print(request.session['user_email'])
context_dict = {}
return render(request, 'dashboard/index.html', context_dict)
My project url file:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^job/', include('job.urls')),
url(r'^welcome/', include('welcome.urls')), //the app for logging in
url(r'^$', include('dashboard.urls')), //main dashboard app
# s
]
My dashboard app url file:
urlpatterns = [
url(r'$', views.index, name='index'),
url(r'^signup/$', views.signup, name='signup'),
url(r'^login/$', views.auth_login, name='login'),
url(r'^logout/$', views.user_logout, name='logout'),
]
When I try and logout and then go to / I get a message saying that http://127.0.0.1:8000/welcome?next=/ doesn't match any urls in my project urls file. So the login check is working, it just can't figure out the url when the GET variable next is set.
As you are using django's default login from contrib.auth, try passing next as an extra_context dict.
url(r'^login/$', 'django.contrib.auth.views.login',
{'template_name': 'login.html', 'extra_context': {'next':'/'}})
Another solution is to specify login url in login_required decorator itself
#login_required(login_url='/login/')
def index(request):
print(request.session['user_email'])
context_dict = {}
return render(request, 'dashboard/index.html', context_dict)
Let know if this solve your issue. HTH :)
I finally got it to work. This is my routes:
Welcome app routes (app for logging in/out)
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^signup/$', views.signup, name='signup'),
url(r'^/login/$', views.auth_login, name='login'),
url(r'^/logout', views.user_logout, name='logout'),
]
Main routes for the entire project:
urlpatterns = [
url(r'^welcome', include('welcome.urls')),
url(r'^', include('dashboard.urls')),
]
Now I don't get any errors with the next variable in the url:
http://127.0.0.1:8000/welcome?next=/
I have a javascript file grabbing the value of next and redirecting to it if the user logs in successfully. *I use ajax to authenticate the user.

app name appearing twice in url | Django

On my home page I have a link to a registration page.
In my browser, the home page is at 127.0.0.1:8000/app_name/
When I click the link to register, it takes me to 127.0.0.1:8000/app_name/app_name/register/, which gives a 404 error.
But it should take me to 127.0.0.1:8000/app_name/register/, which displays the proper html.
In my views.py, for the Registration page I have:
return render(request, 'app_name/register.html', {'form': form})
In my app urls.py, for the Registration page I have:
url(r'^register/$', views.register, name='register'),
Why am I getting the duplicate app_name in my url?
Update:
Here is my Project urls.py:
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^app_name/', include('app_name.urls')),
url(r'^admin/', include(admin.site.urls)),
)
Here is my app_name urls.py:
from django.conf.urls import patterns, url
from app_name import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^register/$', views.register, name='register'),
)
Please add templates dir inside of your project dir and add this path in settings.py(you can add absolute and relate path) add your html pages in this templates folder
read my answer to add path in settings.py
and in your html for link to registration page
Sign Up
your view
return render(request, 'register.html', {'form': form})
Are you declaring the url in your app urls.py or the projects? (I'm wondering how many patterns declarations it's going through)

Django urls.py not found

Setting up the urls for a Django app called OmniCloud_App. Getting and error when accessing /OmniCloud_App/signup that the url is not found. here is the main urls.py:
urlpatterns = patterns('',
(r'^OmniCloud_App/$', include('OmniCloud_App.urls')),
(r'^admin/', include(admin.site.urls)),
)
which then includes OmniCloud_App/urls.py:
urlpatterns = patterns('OmniCloud_App.views',
(r'^', 'home'),
(r'^signup/', 'signup'),
(r'^(?P<User_id>\d+)/$', 'profile'),
(r'^(?P<User_id>\d+)/social$', 'social'),
(r'^(?P<User_id>\d+)/news$', 'news'),
(r'^(?P<User_id>\d+)/email$', 'email'),
(r'^(?P<User_id>\d+)/photos$', 'photos'),
)
so signup should go to the signup method in views.py:
def signup(request):
return render_to_response('OmniCloud_App/Templates/OmniCloud/signup.html', context_instance=RequestContext(request))
Any reason why this won't work? Here is the 404, which implies that it never got past the initial urls.py file, although visiting simply /OmniCloud_App/ renders the 'home' page correctly (which is also defined in the include('OmniCloud_App.urls')
You need to remove the $ from here
(r'^OmniCloud_App/$', include('OmniCloud_App.urls')),
so that it's:
(r'^OmniCloud_App/', include('OmniCloud_App.urls')),
The $ means end of string.

Django redirection

I have a basic splash page, and I am trying to redirect all urls to the splash EXCEPT for the thank you page (which is linked to after the email form is submitted).
How do I make it such that all my urls will redirect to the splash page with the exception of this one page? Currently, ALL of my urls are re-directing, even the exception. Here is my code:
urlpatterns = patterns('',
(r'^$', 'index'),
(r'^thanks/$', 'thanks'),
(r'^', 'index_redirect'),
Thank you.
In Django 1.3 you can use the redirect_to along with a pattern that matches everything.
from django.views.generic.simple import redirect_to
urlpatterns = patterns('',
(r'^$', 'index'),
(r'^thanks/$', 'thanks'),
(r'^.*$', redirect_to, {'url': '/'}),
)
WARNING: this WILL match your static resources and images etc.