How to make this code working django url path - python

My code is not working, and I am not sure why.
I have the problem with hours_ahead function.
Here is urlpatterns path('time/plus/(\d{1,2})/', hours_ahead),
And I imported hours_ahead too

One of these may work
In your urls.py add something like:
urlpatterns = [
path('admin/', admin.site.urls),
path('hours/<int:offset>/', hours_ahead)
]
the "int:offset" is a way of saying what is the parameter you are going to receive and the type.
in settings.py add:
INSTALLED_APPS = [ # careful to not create another INSTALLED_APPS,add the app to the list!
# ...
'playground'
]
Also, I got this error on my terminal:
CommandError: 'playgrounds' conflicts with the name of an existing Python module and cannot be used as an app name. Please try another name.
I could not run the code until I created and app with another name.

Related

Why is app.urls not found?(Django Python)

I’m making my first django based backend server and I registered the urls for each app like
urlpatterns = [
path('admin/', admin.site.urls),
path('backfo/', include('backfo.urls')),
path('commun/', include('commun.urls')),
path('starts/', include('starts.urls')),
path('travleres/', include('travleres.urls')),
path('starts/', include('starts.urls')),
path('new/', include('new.urls')),
path('joint/', include('joint.urls')),
]
Then I get this error in cmd saying ->
ModuleNotFoundError: No module named 'backfo.urls'
I don’t get what went wrong and if you need more code I’ll post it on.
In settings.py add module in INSTALLED_APPS

can anyone help me, why I am getting food is not defined error ? I have attached the structure of Django module

I have added food app. inside food app i added url.py. TO import food.url.py i used include method to import food.url.py in mysite.url.py. but while running the server i am getting error as food is not defined
you does not require to add from mysite import food
just add path('', include('food.urls') where food.urls in single quotation
Make sure food app added in your INSTALLED_APPS list in settings.py
Pass argument in strings of include function like need not be import your apps
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('food.urls')), #change in this line
]
In this URLs pattern pass argument in include method as string like include ('food.urls')
In this file not need the import a food apps but see the your import statment syntax is wrong if you use this way python interpreter will find __init__.py file in module so create please refer documention of python module or see the following enter link description here
In above file first of all you don't need to add from mysite import food
And in your url_patterns the second urls is giving like this:
path('',include('food.urls'))
just add one single quotes..

django warning urls.W005 URL namespace isn't unique

I am having trouble understanding the following warning. I have a namespace called "v1" and I am using these namespaces to determine versioning in my API (using django rest framework). So, I have paths like these:
/v1/accounts/me
/v1/listings
Here is the URLs configuration (project/urls.py):
urlpatterns = [
path('admin/', admin.site.urls),
path('v1/accounts/', include('accounts.urls', namespace='v1')),
path('v1/listings/', include('listings.urls', namespace='v1'))
]
accounts/urls.py
app_name = 'accounts'
urlpatterns = [
url(r'^token/$', views.obtain_auth_token, name='obtain_token'),
url(r'^me/$', my_account, name='my_account'),
]
listings/urls.py
app_name = 'listings'
urlpatterns = [
path('', recent_listings, name='recent_listings')
]
Everything works as expected. All urls are dispatched. Versioning works. However, I keep getting the following error:
?: (urls.W005) URL namespace 'v1' isn't unique. You may not be able to reverse all URLs in this namespace
I know it is a warning and I might be able to suppress it; however, I want to understand why this is happening. Based on my URLconf and this warning, it seems like there cannot be multiple namespaced paths as "siblings." They need to be children of one namespaced path (e.g "v1"). If my understanding is correct, how am I supposed to create this URL configuration.
Basically what happens is that, namespace plays a significant role on reverse finding the url. For example:
In your example reverse('v1:obtain_token') will return /v1/accounts/token/. Lets say you have two urls with same name in accounts and listings, then you might not be able to find accounts url in reverse query. That is why the warning is for. Better if you use different namespaces for each include. In you case, it should be:
path('v1/accounts/', include('accounts.urls', namespace='accounts')),
path('v1/listings/', include('listings.urls', namespace='listings'))
Please read the documentations for more details.
Update
you can do the versioning like this:
path('accounts/', include('accounts.urls', namespace='accounts')), # accounts url
inside accounts app:
path('v1/token/', views.obtain_auth_token, name='obtain_token_v1'),
path('v2/token/', views.obtain_auth_token2, name='obtain_token_v2'),
...

Error in URL configuration trying to set up django-contact-form

I have installed the django-contact-form module and modified my urls.py file at the root level of my django app to contain:
from django.conf.urls import include, url
urlpatterns = [
# ... other URL patterns for your site ...
url(r'^contact/', include('contact_form.urls')),
]
as per the instructions listed here:
https://django-contact-form.readthedocs.io/en/1.2/quickstart.html
But when I try and run my Django app, I get the following error:
RuntimeError: Model class django.contrib.sites.models.Site doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.
I haven't added 'contact_form' to the INSTALLED_APPS setting as the guide explains that is not necessary.
Does anyone know what might be happening? Without that line in my urls.py file, the app runs fine.
Have you tried adding django.contrib.sites to the INSTALLED_APPS list in your settings.py?

Trying to trace a circular import error in Django

I understand circular import error has been asked about a lot but after going through these questions I haven't been able to solve my issue. When I try to run my server in Django its giving me this error message:
The included URLconf module 'accounts_app' from path\to\myproject\__init__.py does not appear to have any patterns in it. if you see valid patterns in the file then the issue is probably caused by a circular import.
The issue started when i added a new app which has a urls.py like the following
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^signin$', views.signin, name='signin'),
url(r'^signout$', views.signout, name='signout'),
url(r'^signup$', views.signup, name='signup'),
]
My project urls.py has a line which points to the app and looks like the following code
urlpatterns = [
url(r'^accounts/', include('accounts_app')),
]
My view looks like the following:
from django.shortcuts import render
from django.http import HttpResponse
def signin(request):
return HttpResponse("<p>This the signin view</p>")
def signout(request):
return HttpResponse("<p>This the signout view</p>")
def signup(request):
return HttpResponse("<p>This the signup view</p>")
Can anyone please help me identify were I could possibly be going wrong.
for those who have the same error but still hasn't debugged their code, also check how you typed "urlpatterns"
having it mistyped or with dash/underscore will result to the same error
Try changing
urlpatterns = [
url(r'^accounts/', include('accounts_app')),
]
to
urlpatterns = [
url(r'^accounts/', include('accounts_app.urls')), # add .urls after app name
]
Those habitual with CamelCased names may face the error as well.
urlpatterns has to be typed exactly as 'urlpatterns'
This will show you error -
urlPatterns = [
path('', views.index, name='index'),
Error -
django.core.exceptions.ImproperlyConfigured: The included URLconf '<module 'polls.urls' from '...\\polls\\urls.py'>' does not appear to have any patterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import.
However, fixing the CamelCase will work -
urlpatterns = [
path('', views.index, name='index'),
]
After 1 hour search it appears that it's wrong speelling it should be : urlpatterns
urlpatterns = [
path('', views.index, name="index")
]
In my case, I got this error because I called the reverse function for a url that required a slug parameter without placing the correct parameter in it.
Once I fixed the reverse function, it was resolved.
In my case I was getting error because I was giving wrong path of dir containing urls. So I changed this
urlpatterns = [
url(r'^user/', include('core.urls'))
]
to this
urlpatterns = [
url(r'^user/', include('core.urls.api'))
]
In my case i was getting this error because i was typing urlpatterns in urls.py to urlpattern.
This error also appears if SomeView doesn't exist in views.py and you do this:
from someapp import views
urlpatterns = [
path('api/someurl/', views.SomeView.as_view(), name="someview"),
]
So make sure all Views you use in urls.py exist in views.py
In my case, I was getting
/SLMS_APP1/urls.py'>' does not appear to have any patterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import.
I made a typo in 'urlpatter'
urlpatter = [
path('',views.index, name='index'),
]
where in correct spelling has to be 'urlpatterns'
urlpatterns = [
path('',views.profile, name='profile'),
]
this error basically occurs when you have, include the app in the main urls.py file and haven't declared urlpattern= [] in-app file.
In my case I changed "reverse" for "reverse_lazy" on my success_url and magially it works.
I changed this...
app_name='users'
urlpatterns=(
path('signup/', SignupView.as_view(),name='signup')
)
to this...
app_name='users'
urlpatterns=(
path('signup/', SignupView.as_view(),name='signup'),
)
notice the last COMMA

Categories

Resources