app name appearing twice in url | Django - 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)

Related

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 2.2 default login url

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.

How to navigate from current html page to another html page in django

I'm working on django app and i'm facing a problem when i need to navigate from my index.html page to another about.html page. I'm setting everything in this way below:
urls.py (myproject)
from django.urls import path, include
urlpatterns = [
path('', include('weather_app.urls')),
path('about/', include('weather_app.urls'))
]
urls.py (myapp)
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('about/', views.about, name="about")
]
In index.html, everything is working well, so i will not put the code.
and in view.py:
from django.shortcuts import render
import requests
def about(request):
return render(request, "about.html")
i have the code below in my index.html:
About
and i cannot get about.html page when click About link as you can see above.
Can anybody help me?
Thanks in advance..
You have to use this tag to avoid manual rendering of urls.
In your code:
About
Let me know if this works!

django tutorial 3 indexpage missing

i am learning django by using django 1.6 documents tutorial 1 - 6.
this round is my 4th try and, previous 3 try was successful and i understand more on every try.
i am in tutorial 3 now, to create views.
according to the documents, after i created a view, i need to map it to a URL.
so i follow the documents to add a urls.py in the polls directory.
and then i follow the document to add include() to mysite/urls.py
i am able to so called wired an index view into the polls view.
now if i go back to localhost:8000, i get an error page,
so my question is
1) WHY?
2) how to get back my localhost:8080 index page co-exist with the polls index page?
thank you very much everyone for your time. i am new and sorry for so simple question.
thank you.
updated:
this is my urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'mysite.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^polls/', include('polls.urls')),
)
this is my polls/urls.py
from django.conf.urls import patterns, url
from polls import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index')
)
the error msg is:
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^admin/
^polls/
The current URL, , didn't match any of these.
http://localhost:8000 index page will be displayed when you created a project (mysite). After creating an application and including it in installed apps in settings.py, you will no longer view the index page of the project, rather your admin page of the application (polls) will be visible at http://localhost:8000/admin
And if you had created any views with the following patterns in polls/urls.py
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^blog/$', views.blog, name='blog'),
)
You need to visit http://localhost:8000/polls for your index page and http://localhost:8000/polls/blogs for your poll blogs page
Editing my answer for step by step approach:
create index.html at polls/templates/polls directory.
in polls/views.py
from django.shortcuts import render_to_response
def pollindex(request):
return render_to_response('polls/index.html', context_instance=RequestContext(request))
in polls/urls.py
urlpatterns = patterns('',
url(r'^index/$', views.pollsindex, name='index'),
)
in your project/urls.py
urlpatterns = patterns('',
url(r'^polls/', include('polls.urls')),
url(r'^admin/', include(admin.site.urls)),
)
And now you can view your template index.html at http://localhost:8000/polls/index

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.