Django URLconf error - python

I am trying to build a website with Django. I am new to Django and have followed the Tango with Django tutorial.
I keep getting an URlconf error I do not understand.
I have a domain (www.example.com), an app (mainApp) and two views in mainApp (homePage, registration).
I want
http://www.example.com to be matched with the homePage view
http://www.example.com/registration/ to be matched with the registration view.
My urls.py file for the project is
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns(
'',
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('mainApp.urls')),
)
The urls.py file for mainApp is
from django.conf.urls import patterns, url
from mainApp import views
urlpatterns = patterns(
'',
url(r'^$', views.homePage, name='homePage'),
url(r'^registration/$', views.registration, name='registration'),
)
This configuration displays the homePage view correctly, but not the registration view. The error is:
Using the URLconf defined in myProject.urls, Django tried these URL
patterns, in this order:
^admin/
^$
^/
The current URL, registration/, didn't match any of these.
What causes the error?

It looks fine. If you're hosting it online you should restart the webapp, if hosting it locally then restart the local host. Without restarting it the changes don't register.

Related

The current path, imageupload, didn't match any of these, Page not Found Django

I am new to Django and I am trying to build an image classifier app, I just tried building the app but I am getting this error, how may I resolve it?
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/imageupload
Using the URLconf defined in imageclassifier.urls, Django tried these URL patterns, in this order:
imageupload ^$ [name='home']
admin/
The current path, imageupload, 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.
These are all changes I made in my app:
This is the views.py file in imgUpload app
from django.shortcuts import render
# Create your views here.
def home(request):
return render(request, 'home.html')
This is the urls.py file in imageUpload app
from django.contrib import admin
from django.urls import path,include
from . import views
urlpatterns = [
path('^$', views.home, name ='home'),
]
And this is the urls.py in the Imageclassifier folder:
"""imageclassifier URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/3.1/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
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 import path,include
urlpatterns = [
path('imageupload', include('imgUpload.urls')),
path('admin/', admin.site.urls),
]
Also, I have added a home.html file in the templates folder. How may I fix this error?
In the latest django path methods, You do not need to use ^$ in your path. Simply remove that ^$ from your path and it will work fine.

How to fix url in python custom login

I have 2 django app. user and management. I have added dashboard/ in main app urls which redirects to user app urls. For this we need to login. All urls in user app is: dashboard/ and for login dashboard/login
but it shows error in /login.
ERROR:
Page not found (404)
Request Method: POST
Request URL: http://localhost:8000/login
Main app urls.py
from user import urls as user_urls
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('dashboard/', include(user_urls))
]+static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
User app urls.py
from django.urls import path, include
from nh_user import views as users_views
from management import urls as management_urls
urlpatterns = [
path('login', users_views.user_login, name='user-login'),
path('logout', users_views.user_logout, name='user-logout'),
path('dashboard/', include(management_urls)),
path('', users_views.index, name='user-index'),
]
You specified dashboard/ as the route for all the urls in your users app. Django won't look for these urls unless you include '/dashboard/' in your address bar.
Currently the working path is http://localhost:8000/dashboard/login.
If you want to change your login path to http://localhost:8000/login you need to remove 'dashboard/' as the route in your project urls.py.
from user import urls as user_urls
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include(user_urls))
]+static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
All the routes for the urls in your users app will now be at directly at http://localhost:8000/route-name without anything preceding them.

Django admin site URL routing issues/inconsistencies

I am working on a website on Django 1.10, and I am trying to set up my admin site so the URL is at "/pcari/admin" instead of "/admin"
Here is my root urls.py:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
# Admin site
url(r'^admin/', admin.site.urls),
# Regular site
url(r'^pcari/', include('pcari.urls')),
]
Here is my app urls.py:
from django.conf.urls import url
from django.contrib import admin
from . import views
app_name = 'pcari'
urlpatterns = [
# Admin site
url(r'^admin/', admin.site.urls),
# User-facing views
url(r'^$', views.index, name='index'),
...
]
This setup technically works in that if I visit "127.0.0.1:8000/pcari/admin" I get the admin site, but I also get the admin site if I visit "127.0.0.1:8000/admin", which is something I do not want.
However, if I remove the "url(r'^admin/', admin.site.urls)," line from the root urls.py file, I get a weird error when I try to access "127.0.0.1:8000/pcari/admin":
NoReverseMatch at /pcari/admin/
u'admin' is not a registered namespace
Request Method: GET
Request URL: http://127.0.0.1:8000/pcari/admin/
Django Version: 1.10.5
Exception Type: NoReverseMatch
Exception Value:
in your main urls.py
urlpatterns = [
# Admin site
url(r'^pcari/admin/', admin.site.urls),
# Regular site
url(r'^pcari/', include('pcari.urls')),
]
and remove the admin url from the app urls
However, if I remove the "url(r'^admin/', admin.site.urls)," line from the root urls.py file, I get a weird error when I try to access "127.0.0.1:8000/pcari/admin":
NoReverseMatch at /pcari/admin/
u'admin' is not a registered namespace
Request Method: GET
Request URL: http://127.0.0.1:8000/pcari/admin/
Django Version: 1.10.5
Exception Type: NoReverseMatch
Exception Value:
Reason:
When you remove url(r'^admin/', admin.site.urls), from main urls.py that means you don't have any admin namespace becuase this line registered "admin" namespace that is define in django code. https://github.com/django/django/blob/master/django/contrib/admin/sites.py#L279
this line show django admin will register as "admin" namespace
When you removed and added admin site in "pcari" app that means this app is register namespace "pcari". But in django admin template has hardcode url in template like {% url admin:index %} that gives NoReverseMatch Exception
https://github.com/django/django/blob/master/django/contrib/admin/templates/admin/base.html#L44 there is hardcode admin namespace in django templates that cause error
EDIT:
You added in app urls
app_name = 'pcari'
That's creating this Noreverse Problem. Remove this app name line. Because it will work as pacri namespace if you add this.

Django can't find social auth url pattern

I have tried to set up social authentication for steam login button. I have followed all these steps, and added steam necessary stuff: adding API key to settings and steam.SteamOpenId to authentication backends.
The error:
Using the URLconf defined in Test.urls, Django tried these URL patterns, in this order:
^ ^$ [name='index']
^login/(?P<backend>[^/]+)/$ [name='begin']
^complete/(?P<backend>[^/]+)/$ [name='complete']
^disconnect/(?P<backend>[^/]+)/$ [name='disconnect']
^disconnect/(?P<backend>[^/]+)/(?P<association_id>[^/]+)/$ [name='disconnect_individual']
^admin/
Hyperlink i used to make login button:
Login
urls.py file of project:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^', include('Home.urls')),
url('', include('social.apps.django_app.urls', namespace='social')),
url(r'^admin/', admin.site.urls),
]
urls.py file of application Home:
from django.conf.urls import url
from . import views
app_name = 'Home'
urlpatterns = [
url(r'^$', views.index, name='index'),
]
Full Project Here.
I am wondering if there is still something that i need to set up, do i need to add views to url patterns? If so, then which? where is social authentication urls file located?

Django Admin Urls Not Working

Some of my django urls will not work. I can visit my index page and the django admin page just fine. I can log in and create objects no problem. For example, I can create a site, like below, but when I try to visit the collection of sites, I get hit with a 404 (even after creating one). I'm using Django allauth as well, and those pages are also having troubles with my urlconf. Can anyone spot the error?
For example, this url:
Page not found (404)
Request Method: GET
Request URL: http://myapp.com/admin/sites/site/
No reward found matching the query
And this one:
Page not found (404)
Request Method: GET
Request URL: http://shielded-island-1440.herokuapp.com/accounts/profile/
Using the URLconf defined in litherewards.urls, Django tried these URL patterns, in this order:
^ ^$ [name='index']
....
^ ^reward/$ [name='reward_list']
^ ^redeem/(?P<reward_code>)/$ [name='redeem_reward']
^ ^redeem/(?P<slug>\w+)/(?P<reward_code>\w+)/$ [name='reward_confirmation']
....
^account/
^sitemap\.xml$
^admin/
My urlconf is as follows:
from django.conf.urls import patterns, include, url
from myapp.views import RewardSitemap
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
admin.autodiscover()
sitemaps = {'rewards':RewardSitemap}
urlpatterns = patterns('',
url('^', include('myapp.urls', namespace="fluffy")),
url(r'^account/', include('allauth.urls')),
url(r'^sitemap\.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps}),
url(r'^admin/', include(admin.site.urls)),
)
The myapp urls included in the first urlconf:
urlpatterns = patterns('',
url(r'^$', IndexView.as_view(), name="index"),
....
url(r'^reward/$', RewardsList.as_view(), name="reward_list"),
url(r'^redeem/(?P<reward_code>)/$', RedeemReward.as_view(), name="redeem_reward"),
url(r'^redeem/(?P<slug>\w+)/(?P<reward_code>\w+)/$', RedeemConfirmation.as_view(), name="reward_confirmation"),
)
And finally, my ROOT_URLCONF is set to myapp.urls. The index page works just fine.
From the limited knowledge that i have, i see that the URl pattern has "account/" and you are accessing "http://shielded-island-1440.herokuapp.com/accounts/profile/" which won't match any of the regex patterns in your URLConf.
Can you try modifying to url(r'^accounts/', include('allauth.urls')), ?

Categories

Resources