Why is my urls.py file not being detected? - python

I just created a new app for my django project and have created a urls.py, models.py, views.py, and serializers.py. However, my project is not detecting the URLs in the urls.py file. If I go to a different app in my project and edit the urls.py file, the system detects it. However, it will not detect it in the new one. Here is the urls.py file:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^knorket-kaitongo-integration/$', views.KnorketKaitongoIntegration.as_view(),
name='knorket_kaitongo_integration'),
]
Does anyone know why this wouldn't be detected? It's not showing up on the API screen either.

Django does not autodetect any files
You need to install them using the installed_apps list and register URLs.
In the Root application, you should find a urls.py file, in that file you need to register your URLs.
You need to import application URLs in Root urls.py and add them in something like this.
urlpatterns = [
path('^', include(urls))
]

Related

Cannot import my application in url.py file in Django

I am completely new in Django and currently learning it from Udemy course.
First, I created my Django project and then created my application. I have inserted my application on settings.py in INSTALLED_APPS of project folder and then re run the the server as well.
Now, I have created a view in my application folder and want to use that in url.py of project folder. I could not able to import it as its showing that package not found.
Please see my program structure below:
I have written the code on pycharm IDE. My code in url.py is below
from django.contrib import admin
from django.urls import path
from firstApp import views
urlpatterns = [
path('^$', views.index, name='index'),
path('admin/', admin.site.urls),
]
Try changing path('^$', views.index, name='index') to path('^$', include('firstApp.urls'), namespace='index')
And you need to import include from django.urls

Page not found 404 - Django

I'm a newbie to Django and I know this probably has been asked alot of times.
So basically what's happening is when I try to create a new project and whenever I'm trying to run my server, by default it's opening http://127.0.0.1:8000/catalog/ and not http://127.0.0.1:8000/.
Even if I run the server with my other projects, I'm facing the same error.
I followed this django basics tutorial on https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/skeleton_website
Idk but somehow I think it's default address is set to http://127.0.0.1:8000/catalog/.
Here's the link to the repo for the project:
https://github.com/Fanceh/django-404-error
Here's my project's urls.py:
from django.contrib import admin
from django.urls import path, include
from testuapp import urls
urlpatterns = [
path('admin/', admin.site.urls),
path('',include("testuapp.urls"))
]
Here's the code in my testuapp urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('', views.testu),
]
Here's my webapp's views.py file:
from django.shortcuts import render
# Create your views here.
def testu(request):
render(request, 'Greetings!')
Is there any way I can change it?
Regards
From the tutorial link it mentions the redirect.
Any request for the root URL, will redirect you to /catalog.
Screenshot from the tutorial below.
HTH
so i assume you are below url pattern structure in your testuapp project.
urlpatterns = [
path('catalog',include("views.catalog"))
]
views.calalog is the name of the method in your view file.
Ok I think I figured it out, it's just the chrome cache. I cleared it and bam it's working!

What is the url.py file that opens home using wagtail?

I'm starting using Wagtail + Django.
Generally, in Django you set the urls path in a urls.py file.
However, I cannot find the url.py that says to my app, when users visits main domain (local http://127.0.0.1:8000/) show home_page.html view.
I'm following the get_started tutorial.
And used this command to generate the basic apps:
wagtail start elim
This generated: a) elim, b) home, c) search apps.
Only elim app contains a urls.py, but it doesn't set a path for home:
from django.conf import settings
from django.conf.urls import include, url
from django.contrib import admin
from wagtail.admin import urls as wagtailadmin_urls
from wagtail.core import urls as wagtail_urls
from wagtail.documents import urls as wagtaildocs_urls
from search import views as search_views
urlpatterns = [
url(r'^django-admin/', admin.site.urls),
url(r'^admin/', include(wagtailadmin_urls)),
url(r'^documents/', include(wagtaildocs_urls)),
url(r'^search/$', search_views.search, name='search'),
]
This is my project structure:
So how does going to "/" open the home page???
The home dir is served by wagtail internal mechanism. Scroll to the end of the file elim/urls.py to find this :
urlpatterns = urlpatterns + [
# For anything not caught by a more specific rule above, hand over to
# Wagtail's page serving mechanism. This should be the last pattern in
# the list:
url(r"", include(wagtail_urls)),
# Alternatively, if you want Wagtail pages to be served from a subpath
# of your site, rather than the site root:
# url(r"^pages/", include(wagtail_urls)),
]
So, continue to read the tutorial, I'm sure you will soon or later discover the Page models and everything provided by wagtail.
Try adding a view function in your app's views.py file that renders home_page.html when the user goes to "/".
in views.py write this:
def home_page(response):
return render(response, "home/home_page.html")
then map this to ur urs.py file
url_patterns = [
path("", views.home_page)
]
then add this url to your url conf

Django templates won't load but HttpResponse will

I recently began learning django and have been running into some trouble and I can't figure out why.
My problem is quite simple: whenever I try to run my homepage with a template, it throws up a 404 error.
My file hierarchy is as such:
crm
accounts
templates
accounts
dashboard.html
urls.py
views.py
crm
urls.py
In crm/urls, I have
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('accounts.urls'))
Then in accounts/urls, there is,
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='home'),
]
And in views, I have
from django.shortcuts import render
from django.http import HttpResponse
def home(request):
return render(request, 'accounts/dashboard.html')
My dashboard.html is just a basic html file with a title and h1, that's it.
Also, whenever I change
return render(request, 'accounts/dashboard.html')
to
return HttpResponse('home')
in my home function, it decides to show.
Your dashboard.html is inside a folder, so it should be referenced like accounts/dashboard.html
Relevant docs: https://docs.djangoproject.com/en/3.0/intro/tutorial03/#a-shortcut-render
In their example, they are using polls/index.html (scroll a bit up from the linked position in the docs). It is analogous to your accounts/dashboard.html in placement (both are in a subfolder as they should be).
Django looks for templates across multiple apps in the same way it searches for static files. Except that you can "see" static files being copied over to a separate folder with manage.py collectstatic while templates get "discovered" in place and not copied anywhere. In facg, if you are running manage.py runserver then the static files are also discovered in place, so the logic is the same.
One difference is that template loading can be changed by using a different set of loaders (docs on template loaders, but by default (and for the majority of projects I guess) this similarity holds.

Refer to my (reusable) app's url namespace in Django

I want to write a reusable Django application.
I tell my users to add the following to their urls.py
path('slack/', include(('slack_integration.urls', 'slack_integration'), namespace='slack_integration'),
And in my urls.py I want to have a view login_callback.
Now in my view, I need to get a value of slack_integration:login_callback.
I can trust the user that he/she will integrate it with slack_integration prefix and use it. But is this the best practise? Can I somehow get the name of the namespace for the app if user chooses a different name for it?
Thanks a lot!
Using namespace= within urls.py files is no longer supported, as it moves something specific to the Django app outside of the Python package that is the Django app.
The best practice now is to define the app_name within the urls.py file inside the Django app.
The old way: DON'T DO THIS (pre-Django 2.0)
the root urls.py
path('slack/', include(('slack_integration.urls', 'slack_integration'), namespace='slack_integration'),
The new way: DO THIS! (Django 2.0+)
the root urls.py
from django.urls import path, include
urlpatterns = [
path('slack/', include(('slack_integration.urls', 'slack_integration')),
]
slack_integration/urls.py
from django.urls import path
app_name = "slack_integrations"
urlpatterns = [
path('', HomeView.as_view(), name='home'),
]
As you can see, this keeps the namespace for the patterns within the app itself, along with the templates most likely to use it. The days of extra instructions on how to include an app are over! Good luck.

Categories