very unexpected IndentationError: unexpected indent in django urls.py - python

This is urls.py for django project here all things are fine:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
]
but when I add my app url:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('movie/', include('movie.urls')),
]
it says
File "/storage/emulated/0/netflix/movie/urls.py", line 5
urlpatterns = [
^
IndentationError: unexpected indent

urlpatterns = [
path('index/', views.index, name='main-view'),]
try using name or relaunch the server

Related

Running Django Server

I need help tried to run server for Django and got this error, The included URLconf '<module 'myapp.urls' from 'C:\Users\user\Documents\Django Tutorial\myproject\myapp\urls.py'>' does not appear to have any patterns in it. If you see the 'urlpatterns' variable with valid patterns in the file then the issue is probably caused by a circular import.
for urls.py in myproject is:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('myapp.urls')),
]
for myapp views.py is:
from django.shortcuts import render
from django.http import HttpResponse
def index(request): return HttpResponse('hello')
For myapp urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]

Error message in Django when I try to runserver - does not appear to have any patterns in it. If you s ee valid patterns in the

Every time I write python manage.py run server. I get the error message:
The included URLconf '<module 'strana.views' from 'D:\\novi projekat\\strana\\views.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.
mysite urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('strana/', include('strana.views')),
]
myapp(named strana)
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
views.py
from django.http import HttpResponse
def index(request):
return HttpResponse('Kita u Bila')
You included your views module. You want to include the module that contains your urls.
from django.contrib import admin from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('strana/', include('strana.urls')),
]

Page not found Error in my Django Project

I just started learning Django and got the following error:
[![The site doesn't see URL and then Error 404][1]][1]
urls.py - site
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('webexample/', include ('webexample.urls')),
]
urls.py - webexample
from django.urls import path
from . import views
urlpatterns = [
path(r'^$', views.index, name='index'),
]
views.py - webexample
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("<h3>This page isn't set up yet...</h3>")
Error photo
change path to this to work for the current url.
path(r'', views.index, name='index')
You could do this
path('index/', views.index, name='index'),
or
re_path(r'^index/$', views.index, name='index'),
to know more about this, you can the django doc https://docs.djangoproject.com/en/3.0/ref/urls/

What is the correct way to link home and about page in django urls?

Right now what I am doing is this:
pages app > urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('about', views.about, name='about'),
]
project > urls.py
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('admin/', admin.site.urls),
path('', include('pages.urls')),
] + static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
This seems to work fine. I am able to go to the homepage and the about page fine, but I have seen other people do as in the following:
project > urls.py
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('admin/', admin.site.urls),
path('', include('pages.urls')),
path('about/', include('pages.urls')),
] + static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
So basically, in the project urls file, the line
'path('about/', include('pages.urls')),'
is added as well as keeping the pages url the same as above.
So I was wondering what is the correct way of linking the about page in the project urls file.
If you have more than a single view and a single url for it in your app and you want to put all of them after some url prefix you put that prefix in urls.py root.
So lets say you have products and categories views and you want them behind api prefix. That means /api/products/ and /api/categories/ then you would have in your "api" app:
from django.urls import path
from . import views
urlpatterns = [
path('products', views.products, name='products'),
path('categories', views.categories, name='categories'),
]
In your root urls.py:
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('admin/', admin.site.urls),
path('api/', include('api.urls')),
] + static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
in pages app you have to create urls.py file and link the url to the view
urlpatterns = [
path('about/', views.about, name='about'),]
and in templates you can call the url with its name

Django-url cmd server error

from django.contrib import admin
from django.urls import path
from adoptions import views
urlpatterns = [
path('admin/', admin.site.urls),
path(r'^$', views.home name='home'),
path(r'^adoptions/(\d+)/', views.pet_detail, name='pet_detail'),
]
this is my code when I run python manage.py runserver...
It returns a syntax error.. saying third line in urlpatterns "name" error which is (name='home') here..

Categories