url issues with django - python

I can't get my urls to work for django no matter what I try
from django.contrib import admin
from django.urls import include, re_path
from django.conf.urls import url
from catalog import views
urlpatterns = [
re_path(r'^admin/', admin.site.urls),
re_path(r'^fruit/?$', views.fruit_catalogue, name='fruit_catalogue'),
re_path(r'^fruit/?$', views.fruit_details),
re_path(r'^fruit/(?P<fruit_id>[1-7]{1})/?$', views.fruit_details),
]

There are 2 url.py files that you should check for the correct functioning of the URLs.
First the one, that is in your project, that should look something like this:
from django.contrib import admin
from django.urls import include, path
from django.conf import settings
urlpatterns = [
path('admin/', admin.site.urls),
path("", include("NAMEOFAPP.urls")),
]
and the other is inside your app. and a simple version of it should look something like this:
from django.urls import path
from . import views
urlpatterns = [
path("home", views.index, name="index"),
path("login", views.login_view, name="login"),
path("register", views.register, name="register")
]
Try to use this same setup and you should be perfectly fine.

Related

django in production enviorment, urls.py only matching empty path i.e " " and nothing else?

Everything works fine on development server but in production server I can only access views.home at https://www.host.com/ and everything else such as
https://www.host.com/b/ or https://www.host.com/tab or https://www.host.com/ext returns status_code 500.
I'm using django 2.1 in python 3.7. Cheers!
Here's the project level urls.py
from django.contrib import admin
from django.urls import path, include
from django.contrib.auth import views as auth_views
from ext.forms import CustomAuthForm
from mysite import settings
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns = [
path('admin/', admin.site.urls),
path('login/', auth_views.LoginView.as_view(authentication_form = CustomAuthForm), name='login'),
path('logout/', auth_views.LogoutView.as_view(next_page= settings.LOGOUT_REDIRECT_URL), name='logout'),
path('', include('ext.urls')),
]
urlpatterns += staticfiles_urlpatterns()
And app level urls.py looks like this
from django.urls import path, re_path
from django.conf.urls import url
from ext import views
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
app_name = 'ext'
urlpatterns = [
path('', views.home, name='home'),
path('int/', views.get_int_status, name='home-load-int'),
path('ext/', views.get_ext_status, name='home-load-ext'),
path('tab/', views.get_tab_status, name='home-load-tab'),
path('b/', views.uzair, name='home-uzair'),
]
I was missing RewriteEngine On in my .httaccess file, what a shame xD

How do I create multiple urls.py in django app?

I want to split the django app urls into two urls with different names, urls.py and reset_urls.py. First one, urls.py works as expected, but the second does not. I tried the obvious approach but it seems not to be working. I want the reset_urls.py to be an endpoint for a password reset, but after creating, it seems not to be working. I know django allows for renaming and having multiple urls.py in single app, but I'm not sure exactly how it should be done, even after checking the docs(not sure I checked the right one)
Here the source code for the urls:
URLconf:
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('', include('blog.urls', namespace='blog')),
path('accounts/', include('users.urls', namespace='account')),
path('accounts/reset', include('users.reset_urls')),
path('admin/', admin.site.urls),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urls.py
Works as expected. no issue here.
from django.urls import path
from users import views
from django.contrib.auth import views as auth_views
app_name = 'account'
urlpatterns = [
path('profile', views.profile, name='profile'),
path('register', views.register, name='register'),
path('login', auth_views.LoginView.as_view(template_name='users/login.html'), name='login'),
path('logout', auth_views.LogoutView.as_view(template_name='users/logout.html'), name='logout'),
]
reset_urls.py
Does not work as expected
from django.urls import path
from django.contrib.auth import views as auth_views
urlpatterns = [
path('password_reset/', auth_views.PasswordResetView.as_view(template_name='users/account/password_reset.html'),
name='password_reset'),
path('password_reset/done/', auth_views.PasswordResetDoneView.as_view(template_name='users/account/password_reset_done.html'),
name='password_reset_done'),
path('password_reset_confirm/<uidb64>/<token>/', auth_views.PasswordResetView.as_view(template_name='users/account/password_reset.html'),
name='password_reset_confirm'),
path('password_reset_complete/', auth_views.PasswordResetView.as_view(template_name='users/account/password_reset.html'),
name='password_reset_complete'),
]
Create an urls folder with __init__.py in it, in app directory. Then seperate your urls as you like into several files in the urls folder.
In __init__.py import your urls like:
from .reset_urls import urlpatterns_reset
from .normal_urls import urlpatterns_normal
urlpatterns = urlpatterns_normal + urlpatterns_reset

getting name error when try to define urls in Django

I want to define URLs in Django but get an error:
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index, name="index"),
]
the error is:
nameError(name'view' is not defind)
please inform me.
thanks,
Saeed
You need to import views. If you have an app named myapp and it has views.py inside the app directory, then import it like this:
from django.contrib import admin
from django.urls import path
from myapp import views # <--- Here
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index, name="index"),
]

Django URL Does not Exist?

This is very strange, any ideas?:
Here is my code for urls.py
from django.contrib import admin
from django.conf.urls import include
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
path('videos/', include('videos.urls'))
]
And videos/urls.py:
from django.urls import path
from . import views
urlpatterns = [
path(r'^$', views.index, name='index'),
]
Actually, I fixed it! Simple mistake. Change videos\urls.py to this;

Module Not FoundError Python Django

I am trying to map the base URL to my "Learning Logs's" home page. here is the following code I have in my main urls.py file:
from django.contrib import admin
from django.urls import path
from django.urls import include
urlpatterns = [
path('admin/', admin.site.urls),
path(r' ', include('learning_logs.urls', namespace='learning_logs')),
]
I save the file and look at the terminal to see if there are any issues and it spits out the following error: ModuleNotFoundError: No module named 'learning_logs.urls'
I am no to python/django and following a book called the python crash course. not sure what I am doing wrong please help!
Best solution is check author's updates:
http://ehmatthes.github.io/pcc/chapter_18/README.html#updates
updates for urls.py
from django.urls import path, include
from django.contrib import admin
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('learning_logs.urls')),
]
Updates for learning_logs/urls.py
"""Defines url patterns for learning_logs."""
from django.urls import path
from . import views
app_name = 'learning_logs'
urlpatterns = [
# Home page.
path('', views.index, name='index'),
]

Categories

Resources