user1592380
user1592380

Reputation: 36205

Calling a python script from a django project

I'm working my way through https://docs.djangoproject.com/en/dev/intro/tutorial03/. In this there is a discussion of calling a view and connecting the a url to a view. Can this also work for a django app or a script that does not contain a view?

Let's assume that if I had an app called 'myapp' with a script called 'myscript', I assume I would do something like

myapp/urls.py

from django.conf.urls import patterns, url
from myapp import myscript

urlpatterns = patterns('',
    url(r'^$', ???myscript???, name='myapp')
)

does this conform to best practices, or if not how should I call the script from inside a django project

Upvotes: 0

Views: 932

Answers (2)

Daniel Roseman
Daniel Roseman

Reputation: 599470

There is nothing special about a Django view: it's simply a function (or other callable) that takes a request and returns a response. There's no limitation on where the function lives, what it's called, what its module is called, whether it's inside a Django app, or anything.

That said, if your "script" is a function that doesn't do either of those things, you probably want to have a separate view that does, and which calls your separate function. Again, you can simply import that within your views file, and call it directly inside the view.

Upvotes: 5

Rohan
Rohan

Reputation: 53316

Django expects a function to handle particular url and with some constraints like:

  • First parameter passed is HttpRequest object
  • The function must return appropriate HttpResponse object.

It does not matter where the function resides either in views.py or any other python file.

Upvotes: 2

Related Questions