django home page path not found - python

enter image description heredjango stopped showing me the admin page
so i used "make migrations" it worked
now it won't show me a "hello world" page
views.py
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("hello world")
urls.py
from django.urls import path
from . import views
urlpatterns = [
path("",views.index,name="home"),
]
urls.py for the app
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path("products/",include('products.urls')),
path('admin/', admin.site.urls),
]
enter image description here

Related

URL pattern not being identified by Django

I followed a tutorial by a youtuber known as Mosh, I followed along with his Django tutorial but whenever I enter the URL pattern, it gives me error 404, page not found. This is a screen shot of the explorer tab.
I only edited the views.py, products.urls.py and pyshop.urls.py files.
views.py:
from django.http import HttpResponse
from django.shortcuts import render
def index(request):
return HttpResponse("Welcome to the new products page!")
def new(reqest):
return HttpResponse("New Products")
products.urls.py:
from django.urls import path
from . import views
urlpatterns = [
path("", views.index),
path("new", views.new)
]
pyshop.urls.py:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path("products/", include("products.urls"))
]
the way you have it should work if you have the url as
localhost:8000/products
#or
localhost:8000/products/new

There is a circular import error shown in the terminal while compiling the code in DJANGO

This is the code in the urls file in project folder 'helloproject':
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('',include(helloapp.urls))
]
This is the code in the urls file in app 'helloapp':
from django.urls import path
from . import views
urlpatterns=[
path('',views.fun,name='fun')
]
This is the code in the views file of 'helloapp':
from django.http import HttpResponse
def fun(request):
return HttpResponse("hello world")

how to pass variables in the urls in django?

i tried to pass variables in url in django but it keeps getting 404 error
urls.py
from django.conf.urls import include, url
from django.contrib import admin
from test_url import views
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^detail/<int:id>/',views.detail)
]
views.py
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def detail(request,id):
return HttpResponse("<h1>{}.</h1>".format(i

how to fix this 404 error in django urlconfig patterns

I'm creating a basic app in my Django project. I mapped the views to url. While running this project it is showing
404 page not found.
in urls.py
from django.urls import path
from . import views
urlpatterns=[
path(' ',views.index,name="index"),
]
in views.py
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
return HttpResponse("hello world");
in project/urls.py
from django.urls import include,path
from django.contrib import admin
urlpatterns = [
path("myapp/ ",include('myapp.urls')),
path('admin/', admin.site.urls),
]
I expect the output to be like hello world
Remove space character form your patters
In urls.py change
path(' ',views.index,name="index") to path('',views.index,name="index")
In project/urls.py change
path("myapp/ ",include('myapp.urls')), to path("myapp/",include('myapp.urls')),

How to call a function inside view.py

This is the current project stucture
R6Scorextractor
R6Scoreex
migrations
templates
R6Scoreex
header.html
home.html
__Init__.py
settings.py
urls.py
views.py
models.py
apps.py
admin.py
tests.py
R6Scorextractor
__Init__.py
settings.py
urls.py
manage.py
R6Scorextractor/R6scoreex/urls.py
from django.conf.urls import url
from . import views
from django.conf.urls import include
urlpatterns = [
url(r'^$', views.index, name='index'),
]
R6Scorextractor/R6scoreex/views.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.shortcuts import render
from django.shortcuts import render
from django.conf import settings
from django.core.files.storage import FileSystemStorage
# Create your views here.
from django.http import HttpResponse
import pdb;
def index(request):
return render(request, 'R6scoreex/home.html')
def simple_upload(request):
print "Entered simple_upload"
if request.method == 'POST' and request.FILES['myfile']:
myfile = request.FILES['myfile']
fs = FileSystemStorage()
filename = fs.save(myfile.name, myfile)
uploaded_file_url = fs.url(filename)
return render(request, 'R6scoreex/home.html', {
'uploaded_file_url': uploaded_file_url
})
return render(request, 'R6scoreex/home.html')
R6Scorextractor/R6Scorextractor/url.py
from django.conf.urls import url
from django.contrib import admin
from django.conf.urls import include
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'', include('R6scoreex.urls')),
]
I just want to know how to call simple_upload inside views.py of R6scoreex module.How to write URL for it , server gives me 404 error when I went with the following
url(r'^/simple_upload/$', views.simple_upload, name='simple_upload'),
So what is the reason I am getting 404 error even after adding the above code what am I doing wrong here
You have a Regex problem most probably.
This pattern works for me: (R6Scorextractor/R6Scorextractor/url.py)
from django.conf.urls import url
from django.contrib import admin
from django.conf.urls import include
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^/', include('R6scoreex.urls')),
]
And (R6Scorextractor/R6scoreex/urls.py)
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^simple_upload/', views.simple_upload, name='simple_upload'),
]

Categories

Resources