Django 2.2 default login url - django

I want to load the Django builtin login page, but not with the URL http://127.0.0.1:8000/login/, instead of this i am trying to load the login page like this URL http://127.0.0.1:8000/
I have tried :
LOGIN_URL ='/login/' - (in settings.py)
#login_required()- (in the app view.py)
#login_required(login_url='/login/') - (in the app view.py)
these both method not helped me, kindly someone help me to sort this out.
project url.py
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('home.urls')),
]
project settings.py in the bottom i have included like below
LOGIN_URL='/login/'
LOGIN_REDIRECT_URL= 'home'
LOGOUT_REDIRECT_URL ='home'
in my app url
#
from django.urls import path, include
from home.views import HomeView
urlpatterns = [
path('',include('django.contrib.auth.urls')),
path('home',HomeView, name='home'),
]
in app view.py
from django.http import HttpResponse
from django.shortcuts import render
#login_required(login_url='/login/')
def HomeView(request):
return render(request, "home.html")
overall my expectation to load the Django default Login page without passing /login/ or account/login in the url.

you can use a generic class
from django.contrib import admin
from django.urls import path, include
from django.views.generic.base import RedirectView
urlpatterns = [
path('', RedirectView.as_view(url='/admin/'), name='django_admin'),
path('admin/', admin.site.urls),
path('other/', include('other.urls')),
]
https://docs.djangoproject.com/en/2.2/ref/class-based-views/base/

You can try change '/login/' for '' in all files.

Related

You are seeing this page because DEBUG=True is in your settings file and you have not configured any URLs

the explorer in vscode Getting the error: You are seeing this page because DEBUG=True is in your settings file and you have not configured any URLs.
meetups/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('meetups/', views.index) #domain_name.com/meetups/
]
urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('meetups.urls'))
]
views.py
from django.shortcuts import render
# Create your views here.
def index(request):
return render(request,'templates/meetups/index.html')
You probably didn't add your app (meetups) into settings.INSTALLED_APPS, which can be found in your_project_name/settings.py.
Change meetups/templates/index.html to meetups/index.html also add name in your urlpatterns

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.

im getting error in django Page not found (404) Request Method: GET Request URL: http://127.0.0.1:8000/about/

code of project named carparts urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('parts.urls')),
]
code for my app named parts urls.py
from django.contrib import admin
from django.urls import path, include
from . import views
urlpatterns = [
path('', views.index,name='index'),
path('about', views.about,name='about'),
path('contact', views.contact,name='contact'),
]
code for views.py
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
return render(request, 'index.html')
# return HttpResponse('this is home page')
def about(request):
return render(request, 'about.html')
def contact(request):
return render(request, 'contact.html')
index page code output is show in http://127.0.0.1:8000 but my about and contact page shows Page not found (404) . Can any one help me with this code.
Thank you
Joint '/' in your both urls.py like that:
urlpatterns = [
path('', views.index,name='index'),
path('about/', views.about,name='about'),
path('contact/', views.contact,name='contact'),
]
According Django Design philosophy on Definitive URLs:
Technically, foo.com/bar and foo.com/bar/ are two different URLs, and
search-engine robots (and some Web traffic-analyzing tools) would
treat them as separate pages. Django should make an effort to
“normalize” URLs so that search-engine robots don’t get confused.

Hello. I am having a problem to change the pages in my django project that it shows a page not found (404)

I've been building my first Django project, and I was doing the "login page", and it is working, but when then, I've made a condition that if the login is wrong, it comes back to the login page and shows an error message, and if it is right, it should go to a page where is written "logadissimo", but when I try this last one, I get the problem below:
Page not found (404) Request Method: GET Request
URL: http://localhost:8000/ Using the URLconf defined in sitetcc.urls,
Django tried these URL patterns, in this order:
admin/ login/ login/submit login/logado The empty path didn't match
any of these.
You're seeing this error because you have DEBUG = True in your Django
settings file. Change that to False, and Django will display a
standard 404 page.
this is my url.py from the project:
"""
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls.conf import include
from django.urls import path
from core import views
urlpatterns = [
path('admin/', admin.site.urls),
path('login/', views.login_user),
path('login/submit', views.submit_login),
path('login/logado', views.logado)
]
the urls.py from the application:
from . import views
from django.urls import path
app_name = 'core'
urlpatterns = [
path('', views.login_user, name='login'),
path('', views.submit_login, name='submit'),
path('', views.logado, name='logado')
]
the views.py:
from django.shortcuts import render, redirect
from django.views.decorators.csrf import csrf_protect
from django.contrib.auth import authenticate, login
from django.contrib import messages
# Create your views here.
def logado(request):
return render(request, 'logado.html')
def login_user(request):
return render(request,'login.html')
#csrf_protect
def submit_login(request):
if request.POST:
username = request.POST.get('username')
password = request.POST.get('password')
print(username)
print(password)
user = authenticate(username=username, password=password)
if user is not None:
login(request, user)
return redirect('/logado/')
else:
messages.error(request,'Usuário e senha inválido. Favor tentar novamente:')
return redirect('/login/')
and a print screen with the folders' structure and the page "logado":
https://imgur.com/a/xAsBRHO
Thank you!
try return redirect('logado') and return redirect('login') syntax return redirect('your_view_name')
That's because you're passing a hardcoded URL (/logado/) to redirect that doesn't exists in your project's url.py, so Django can't match that route.
There are a couple of ways you can pass an argument to redirect, you can see some examples here. I strongly recommend you to use the name of your url's view, so in the future you can change the route of your path without any issues.
Also, you can declare your URLs like this, in that way you'll keep things clean and not bloat your project's urls.py.
Doing this you'll have your project's urls.py like this:
from django.contrib import admin
from django.urls.conf import include
from django.urls import path
from core import views
urlpatterns = [
path('admin/', admin.site.urls),
path('login/', include('core.urls'),
]
And your application's urls.py like this:
from . import views
from django.urls import path
app_name = 'core'
urlpatterns = [
path('', views.login_user, name='login'),
path('login/submit/', views.submit_login, name='submit'),
path('login/logado/', views.logado, name='logado')
]
(Don't forget the trailing / at the end of each route)
Now you can call your view like this return redirect('core:logado')

Django Project App Urls Page Not Found (404)

I still new to Django and I'm following this tutorial in YouTube. I can't proceed to the next tutorial because I hit an error.
When I type http://127.0.0.1:8000/music. I get this error.
Check my code below:
mysite urls.py
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^music/', include('music.urls')),
]
music urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
music views.py
from django.http import HttpResponse
def index(request):
return HttpResponse("<h1>Hello World</h1>")
Please help.
You might have not saved the project/urls.py file.
Or to check whether the music is set in INSTALLED_APPS in settings.py?