So, everything in my newly started Django project is working fine except this logout url. I'm using django=1.11.17 and python 3. When I try to logout from the accounts, this is the error I get.
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/%25%20url%20'accounts:logout'%20%25
Using the URLconf defined in conspiverse_project.urls, Django tried these URL patterns, in this order:
^$ [name='home']
^test/$ [name='test']
^thanks/$ [name='thanks']
^admin/
^accounts/
^accounts/
The current path, % url 'accounts:logout' %, 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.
accounts/urls.py
from django.conf.urls import url, include
from django.contrib.auth import views as auth_views
from . import views
app_name = 'accounts'
urlpatterns = [
url(r"login/$", auth_views.LoginView.as_view(template_name="accounts/login.html"),name='login'),
url(r"logout/$", auth_views.LogoutView.as_view(template_name="thanks.html"), name="logout"),
url(r"signup/$", views.SignUp.as_view(), name="signup"),
]
project/urls.py
from django.conf.urls import url, include
from django.contrib import admin
from . import views
urlpatterns = [
url(r"^$", views.HomePage.as_view(), name="home"),
url(r"^test/$", views.TestPage.as_view(), name="test"),
url(r"^thanks/$", views.ThanksPage.as_view(), name="thanks"),
url(r"^admin/", admin.site.urls),
url(r"^accounts/", include("accounts.urls", namespace="accounts")),
url(r"^accounts/", include("django.contrib.auth.urls")),
]
settings.py
STATIC_URL = '/static/'
STATICFILES_DIR = [os.path.join(BASE_DIR,'static')]
LOGIN_REDIRECT_URL = 'test'
LOGOUT_REDIRECT_URL = 'thanks'
I tried verifying the URL paths, cross-checked almost everything, it is driving me crazy. Huge thanks for the help!
Related
I have the following urls.py in my project dir,
Main project urls.py
from django.contrib import admin
from django.urls import path, include
from . import views
urlpatterns = [
path('', views.render_calculator, name='render_calculator'),
path('calculator/', views.render_calculator, name='render_calculator'),
path('disclaimer/', views.render_disclaimer, name='render_disclaimer'),
path('cookiepolicy/', views.render_cookiepolicy, name='render_cookiepolicy'),
path('privacypolicy/', views.render_privacypolicy, name='render_privacypolicy'),
path('dashboard/', views.render_dashboard, name='render_dashboard'),
path('about/', views.render_about, name='render_about'),
path('admin/', admin.site.urls),
path(r'^', include('accounts.urls'))
]
Now I created a new app accounts (I added it to my apps in settings.py) where I would like to store the urls of that app in its own dir like so:
Accounts app urls.py
from . import views
from django.urls import path
urlpatterns = [
path('register/', views.render_register, name='render_register'),
]
Accounts app views.py:
from django.shortcuts import render
def render_register(request, template="register.html"):
return render(request, template)
However, this configuration throws me this error:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/register
Using the URLconf defined in CFD.urls, Django tried these URL patterns, in this order:
[name='render_calculator']
calculator/ [name='render_calculator']
disclaimer/ [name='render_disclaimer']
cookiepolicy/ [name='render_cookiepolicy']
privacypolicy/ [name='render_privacypolicy']
dashboard/ [name='render_dashboard']
about/ [name='render_about']
admin/
^
The current path, register, didn't match any of these.
Where is the missing piece?
You are using path() with the regex r'^' which is causing your problem.
In order to define a path with a regex, you need to use re_path.
So change it to the following line:
re_path(r'^', include('accounts.urls'))
or you can use
path('', include('accounts.urls'))
Change this.
path(r'^', include('accounts.urls')) to
path('', include('accounts.urls'))
Page not found (404)
Request Method:
GET
Request URL:
http://127.0.0.1:8000/
Using the URLconf defined in just.urls, Django tried these URL patterns, in this order:
1. admin/
2. shop/
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.
Here is my code:
Main ecom\urls.py:------------>>>>
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('shop/', include('shop.urls'))
]
Now shop\urls.py:-------------->>>>
from django.urls import path
from . import views
urlpatterns = [
path("",views.index, name="ShopHome")
]
And shop\views.py:-------------->>>>
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("Shop Index")
Please help me guys
The only pages that should be working for you right now is /shop/ and /admin/. There is no root page because you have an include, which always start with /shop/ and checks the shop/urls. Not sure what you want, but if you want the root page to give you the index view, you can change ecom/urls shop url to:
path('', views.index, name="ShopName)
Dont forget to import the views of course
You have not defined blank URL. In main URLs.py, you have used path('shop/', include('shop.urls')), and in shop urls.py path("",views.index, name="ShopHome"). this path will redirect to http://127.0.0.1:8000/shop/ not http://127.0.0.1:8000/.
If you want blank url then do this:
ecom\urls.py:
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('shop.urls'))
]
shop\urls.py
urlpatterns = [
path("",views.index, name="ShopHome")
]
remember whatever you add in path('', include('shop.urls')), it will be added in all URLs written inside shop app.
If you want a blank URL and also "shop" added in rest of the URLs, do this:
From shop import views
urlpatterns = [
path('admin/', admin.site.urls),
path('shop/', include('shop.urls'))
path('',views.index, name="ShopHome")
]
I'm trying to access a URL, http://localhost:8000/calc, but it gives me this error:
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^admin/
^$ [name='home']
^calc/
The current path, calc, didn't match any of these.
This is what I currently have for mysite URL and secondapp URL:
# mysite/urls.py
from django.conf.urls import url, include
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'',include('firstapp.urls')),
url(r'^calc/',include('secondapp.urls')),
]
# secondapp/urls.py
from django.conf.urls import url
from secondapp.views import CalcPage
urlpatterns = [
url(r'calc/', CalcPage.as_view(), name='calc'),
]
As per the URL configuration you have stated in mysite.url and secondapp.url you link would be
localhost:8000/calc/calc/
which IMO would be confusing, if I am correct, URL you want is
localhost:8000/calc/
For that you have to change the url you have defined in secondapp.url to
from django.conf.urls import url
from secondapp.views import CalcPage
urlpatterns = [
url(r'$', CalcPage.as_view(), name='calc'),
]
This would make the ClacPage accessible on
localhost:8000/calc/
See / is very important when defining URLs.
use below reg-ex in urls.py
r'^calc/$'
I'm going through the polls Django tutorial and have searched for answers to this. So far:
I made sure I'm using the right directories. The main urls.py is in mysite/mysite/, the polls urls.py is in mysite/polls/urls.py.
Tried adding 'polls' to INSTALLED_APPS in the settings.py of mysite/mysite.
Am making sure that I am requesting 127.0.0.1:8000/polls and not 127.0.0.1:8000
I am using Python 3.4 and Django 1.9, same as the tutorial.
I am still receiving this message:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/polls
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^polls/
^admin/
The current URL, polls, didn't match any of these.
mysite/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")
In your polls/urls.py
change url(r'^%', views.index, name='index') to url(r'^$', views.index, name='index').
Also in your urls.py, you have route for 127.0.0.1:8000/polls/ and you request for 127.0.0.1:8000/polls . Notice the trailing slash.
So you should request to 127.0.0.1:8000/polls/ .
Add APPEND_SLASH = True in your settings.py file so that it would redirect 127.0.0.1:8000/polls to 127.0.0.1:8000/polls/ .
So i tried your Index view with your urls.py.
It works, the only thing different is in your "Request URL, you must request for - 127.0.0.1:8000/polls/%
changing your urls to
url('^/%', index) - polls URLs.py
url('^polls', include('polls.urls')) - project URLs
This will work.
What am I doing wrong ?
My URL : http://localhost:8000/login/
The DEBUG log from Django :
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/login/
Using the URLconf defined in dealers.urls, Django tried these URL patterns, in this order:
^login|home/ ^login/$
^login|home/ ^home/$
^login|home/ ^home/dealer/(?P<dealer_id>\d+)/$
^admin/
The current URL, login/, didn't match any of these.
urls.py file :
from django.conf.urls.defaults import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^login|home/',include('dealerpanel.urls')),
(r'^admin/', include(admin.site.urls))
)
dealerpanel/urls.py :
from django.conf.urls.defaults import patterns, include, url
urlpatterns = patterns('dealerpanel.views',
(r'^login/$','login'),
(r'^home/$','home'),
(r'^home/dealer/(?P<dealer_id>\d+)/$','details')
)
## urls.py
urlpatterns = patterns('',
(r'',include('dealerpanel.urls')),
(r'^admin/', include(admin.site.urls))
)
Change the prefix for including dealerpanel.urls to be the empty string. I think the way you have it structured it would actually be looking for a url like:
/login/login/
/login/home/
/home/login
...
Add something like this to your dealerpanel/urls.py so that http://localhost:8000/login/ will hit the view dealerpanel.views.target_view:
urlpatterns = patterns('dealerpanel.views',
...
(r'','target_view'),
)
This means the empty string after login (or home) will match the target_view