NoReverseMatch at /new_application/check_login/ - django

I am new to DJango. I am getting error 'NoReverseMatch at /new_application/check_login/' while redirecting view from another view.
different files listed below.
Main urls.py
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^new_application/', include('new_application.urls')),
]
views.py
from django.shortcuts import render, render_to_response
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from .models import Login
...
def check_login(request):
...
return HttpResponseRedirect(reverse('new_application:loggedin',args=(user,)))
def loggedin(request, user):
return render(request, 'new_application/loggedin.html',{'full_name': user.first_name +" "+ user.last_name})
Application urls.py
from django.conf.urls import url
from . import views
app_name = 'new_application'
urlpatterns = [
url(r'^$', views.login, name='login'),
url(r'^check_login/$', views.check_login, name='check_login'),
url(r'^loggedin/$', views.loggedin, name='loggedin'),
url(r'^invalid_login/$', views.invalid_login, name='invalid_login'),
url(r'^logout/$', views.logout, name='logout'),
]
Here is an error image : error image
please give the solution for fixing this error.
Thank You.

The regex pattern must match against the given url...
url(r'^loggedin/(?P<user>\w+)/$', views.loggedin, name='loggedin'),

Reverse should match one of URL names from urls.py
return HttpResponseRedirect(reverse('loggedin',args=(user,)))

Related

onw of my two app is not working in django

Below is my code. In my hello_world project there is two app pages. one is home page and another is profile page. home page is working fine, but profile page is showing error.
hello_world urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('home_page.urls',)),
path('profile_page',include('profile_page.urls',))
]
home page urls.py
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('',views.home,name='home page'),
]
home page views.py
from django.http import HttpResponse
def home(request):
return HttpResponse('home page')
profile page urls.py
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('profile_page',views.profile,name='profile page'),
]
profile page views.py
from django.http import HttpResponse
def profile(request):
return HttpResponse('profile page')
You need to use redirect method and include view name space as an argument..
Find the updated code:
from django.http import HttpResponse
def profile(request):
return redirect('profile page')
Don't forgot to import redirect...
The first argument of the path() function, i.e. the 'route' argument, must end with a forward slash.
path('profile_page/',include('profile_page.urls',))
Notice the forward slash at the end of the first argument, 'profile_page/'.
Your URLconf is not matching the expected pattern because of the missing slash.

Error while using URLconf defined in mysite.urls (Django tutorial)

I know this question has been asked and i have gone through all those answers but i didn't get the solution still. Please help me find out what is wrong .
This is the code from django website for version 1.11 of django .
mysite/urls.py
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^polls/', include('polls.urls')),
url(r'^admin/', admin.site.urls),
]
mysite/polls/urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
mysite/polls/views.py
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
I am getting the following error
Click to view the error
Your urls.py is in the wrong place, it should be mysite/mysite/urls.py (in the same directory as settings.py).

Django: how to access functions in the views.py

I have a few functions in the view.py in my Django project:
Here is the views.py and urls.py under polls:
polls/views.py
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
def search(request):
return HttpResponse("You're at the polls search.")
polls/urls.py
from django.urls import path
from . import views
from django.conf.urls import include, url
urlpatterns = [
path('', views.index, name='index'),
path('', views.search, name='search'),
]
I am able to get the index page, but have trouble to reach the page in the search function. I got the error below:
How do I access the search function in the views.py? Thanks!
edit your polls/urls.py as below:
urlpatterns = [
path('', views.index, name='index'),
path('search/', views.search, name='search'),
]
the first argument of the path is the url pattern.
I think you misunderstood the third argument(name). it has nothing to do with the url pattern, it's a name for the url, that'll be useful for url reversing. read the document for more information

Django redirect user to app based on permission and group

Say i have a django project which have 4 apps 1 app is for logging in
project urls:
from django.conf.urls import url, include
from django.contrib import admin
from django.conf import settings
urlpatterns = [
url(r'^', include('Login.urls')),
url(r'^admin/', admin.site.urls),
]
settings:
LOGIN_REDIRECT_URL = '/'
login app urls:
from django.conf.urls import url
from django.contrib.auth import views as auth_views
urlpatterns = [
url(r'^login/$', auth_views.login, name='login'),
url(r'^logout/$', auth_views.logout, {'next_page': '/'}, name='logout'),
]
now say i have 3 apps based on user type i.e. Admin, Team Lead, Worker
so i want to redirect user based on their type of employment.
how can i achieve this ?
any help is appreciated
I guess that you will manually add/edit the user groups.
That can be done from django admin panel.
Here is the group/permission django docs: Permissions and Authorization
This is what I did:
I've created an index view in order to get the user group and redirect to the respective view that you want.
So, index view in views.py:
from django.contrib.auth.decorators import login_required
#login_required
def index(request):
group = request.user.groups.filter(user=request.user)[0]
if group.name=="employees":
return HttpResponseRedirect(reverse('worker'))
elif group.name=="teamLeader":
return HttpResponseRedirect(reverse('teamLeader'))
elif group.name=="admin":
return HttpResponseRedirect(reverse('adm'))
context = {}
template = "index.html"
return render(request, template, context)
And urls in urls.py:
from django.conf.urls import url
from django.contrib import admin
from django.contrib.auth import views as auth_views
from app import views as app_views
urlpatterns = [
url(r'^login/$', auth_views.login, name='login'),
url(r'^$', app_views.index, name='index'),
url(r'^employees/$', app_views.employees, name='employees'),
url(r'^teamLeader/$', app_views.teamLeader, name='teamLeader'),
url(r'^adm/$', app_views.adm, name='adm'),
]
In settings.py
LOGIN_URL = '/login'
LOGIN_REDIRECT_URL = '/'
Is this what you want to do?

what's wrong with my urls.py in this example?

I just installed userena, and had the example working from the tutorial, but as soon as I added in a single line in URLS.py, I'm getting an error. In the example below, I added the line mapping the home function from views.py
Now the issue I'm having is that when I go to 127.0.0.1/8000, I get TypeError: string is not callable, but then oddly, if I go to accounts/signup or accounts/signin, I am getting the template that should be appearing if i go to 127.0.0.1/8000.
from django.conf import settings
from django.conf.urls import patterns, include, url
from django.conf.urls.static import static
from django.views.generic import TemplateView
from accounts import views
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r"^$", 'home'),
url(r'^admin/', include(admin.site.urls)),
(r'^accounts/', include('userena.urls')),
)
Here is my accounts/views.py
from django.shortcuts import render
from django.http import HttpResponseRedirect
def home(request):
return render('homepage.html')
You need to remove the quotes in the url and import that view
from accounts.views import home
urlpatterns = patterns('',
url(r"^$", home),
url(r'^admin/', include(admin.site.urls)),
(r'^accounts/', include('userena.urls')),
)
You can steel use the strings in the url() but you must use the format 'app.views.viewname'
urlpatterns = patterns('',
url(r"^$", 'accounts.views.home'),
url(r'^admin/', include(admin.site.urls)),
(r'^accounts/', include('userena.urls')),
)
Or name the module in the first argument as string to patterns()
urlpatterns = patterns('accounts.views',
url(r"^$", 'home'),
url(r'^admin/', include(admin.site.urls)),
(r'^accounts/', include('userena.urls')),
)
the issue was I forgot to include the request in the return render.
The correct answer is that render is being called incorrectly. Actually, the views.py file would raise a SyntaxError, but we'll let that slide :)
# views.py
from django.shortcuts import render
def home(request):
return render(request, 'homepage.html')