I am trying to run an enternal python script upon clicking a button in a django website, however I think the path of my external script which i have specified is in the wrong formatting:
from django.shortcuts import render, render_to_response
from subprocess import run,PIPE
import requests
import sys
from django.views.decorators.csrf import csrf_exempt
# Create your views here.
def index(request):
return render_to_response('index.html')
#csrf_exempt
def external(request):
inp=request.POST.get('param')
out=run(sys.executable==['//D://Desktop//new//file1//test.py',inp], shell=False,stdout=PIPE)
print(out)
return render(requests, "index.html",{'data1':out})
I also have an error which says
TypeError at /external/
'bool' object is not iterable
when I run it on the local server.
My urls.py file:
from django.contrib import admin
from django.urls import path
from myapp import views as v
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns = [
path('admin/', admin.site.urls),
path('', v.index, name="index"),
path("external/", v.external),
]
urlpatterns += staticfiles_urlpatterns()
In your view, you wrote:
out=run(sys.executable==['//D://Desktop//new//file1//test.py',inp], shell=False,stdout=PIPE)
But that makes not much sense: you are here comparing sys.executable (which is a string), with a list of strings, and therefore it will return False. So you then call run(False, shell=False, stdout=PIPE), but False is of course not a string.
You can rewrite this to:
out=run([sys.executable, '//D://Desktop//new//file1//test.py',inp], shell=False,stdout=PIPE)
Related
I already search for any answer which could help me before write this question, but I haven't found anything that helps.
The thing is that I follow the tutorial and I can't see the view that I created.
Now I'm going to share my code:
project urls.py:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
polls urls.py:
from django.urls import path
from . import views
urlpatterns = [
path(" ", views.index, name='index'),
#127.0.0.1/polls/
]
polls views.py
from django.http import HttpResponse
from django.shortcuts import render
# Create your views here.
def index(request):
HttpResponse("Welcome to de Polls Universe Index")
OK I ALREADY KNOW WHATS GOING ON:
I forgot the RETURN before de HttpResponse.
There are two issues with your code:
The path for the index view contains a blank, which must be removed
The view function must return a response object. Please add return in front of the last line.
I'm just beginning to learn Django.
I've created a simple web sub-app called 'flavo' inside of another one called 'djangoTest'
When I run http://127.0.0.1:8000/flavo
it correctly displays
Hello, World!
then when I run http://127.0.0.1:8000/flavo/a it should show
Hello, a!
But instead I get:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/flavo/a
Using the URLconf defined in testDjango.urls, Django tried these URL patterns, in this order:
admin/
flavo [name='index0']
flavo a [name='a']
The current path, flavo/a, didn’t match any of these.
in testDjango/hello/views.py I have
from django.http import HttpResponse
from django.shortcuts import render
def index0(request):
return HttpResponse("Hello, world!")
def a(request):
return HttpResponse("Hello, a!")
In testDjango/flavo/url/py I have
from django.urls import path
from . import views
urlpatterns = [
path("", views.index0, name="index0"),
path("a", views.a, name="a"),
]
The only other file I've changed is , testDjango/testDjango/urls.py"
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('flavo', include("flavo.urls")),
]
I'm confused why I can't access http://127.0.0.1:8000/flavo/a
Add '/'.
like this.
# testDjango/testDjango/urls.py
path('flavo/', include("flavo.urls"))
I'm making a django project and whenever I run "python manage.py runserver". I see the above error.
views.py
from django.shortcuts import render
from django.http import HttpResponse
from .models import ToDoList, Item
# Create your views here.
def index(response, id):
ls = ToDoList.objects.get(id=id)
return render(response, "main/base.html", {})
def home(response):
return render(response, "main/home.html", {})
main/url.py
from django.urls import path
from main import views
from . import views
urlpatterns = [
path("<int:id>", views.index, name="index"),
path("", views.home, name="home")
]
mysite/url.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include("main.urls")),
]
Thank you for the help.
main/urls.py
from django.urls import path
from main import views
from . import views
Those last two imports both import the name views, so the first one is overwritten by the second one.
Your home function is indented wrongly, it's currently a function inside the index function, not in the global scope.
Change it to the following
views.py
def index(response, id):
ls = ToDoList.objects.get(id=id)
return render(response, "main/base.html", {})
def home(response): # Don't indent this!
return render(response, "main/home.html", {})
There must be a configuration issue but if you want to see if website is working or def Home(): is working. Change your code to
def home(request):
return HttpResponse('<h1>Blog Home</h1>')
i have created a view in views.py and when i try to add the url i get the error. I am following a tutorial and doing exactly as told.created view
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
return HttpResponse("Hello World!")
adding url
from first_app import views
urlpatterns = [
url(r'^$',views.index,name='index'),
path('admin/', admin.site.urls),
]
tutorial im following
You need to import url so that you can use it in your urls.py
from django.urls import path
from django.conf.urls import url
from first_app import views
urlpatterns = [
url(r'^$',views.index,name='index'),
path('admin/', admin.site.urls),
]
You should be aware that url is the old way of defining url patterns and is likely to be deprecated in the future
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')),