How to call a function inside view.py - python

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'),
]

Related

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")

django home page path not found

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

Using the URLconf defined in lec3.urls, Django tried these URL patterns, in this order: admin/ hello/ The empty path didn't match any of these

these are my main codes i wrote to create wepapp but i get this 404 error so pleas help
my hello urls.py
from django.urls import path
from hello import views
urlpatterns = [
path("", views.index, name="index")
]
my urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('hello/', include("hello.urls")),
]
my views.py
from django.http import HttpResponse
from django.shortcuts import render
# Create your views here.
def index(request):
return HttpResponse("hello, orld")
It looks like you are trying to access index with
http://127.0.0.1:8000/. This will not work, because your index is included in hello's urls.py, and hello's url starts with hello/.
Try
http://127.0.0.1:8000/hello
instead.

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')),

Categories

Resources