BenjiA
BenjiA

Reputation: 23

Call same function from multiple views

I'd like to be able to call a function from different views in django. For example, say I need to generate a random number from within various views, I don't want to have to have the same 'random number' code repeated in each view - I just want to 'call on a function'.

I'm greatly simplifying the following code for the sake of keeping this question brief:

views.py

def viewOne(request):
#code for this view, including needing to generate a random number
import random
myrandomnumber = random.randint(1,21)*5


def viewTwo(request):
#code for this view, including needing to generate a random number
import random
myrandomnumber = random.randint(1,21)*5

As you can see, I'm using the same code in both views to generate a random number. If I wanted to update how I generate a random number, I'd have to update it in both views.

This is the sort of thing I want to do:

views.py

def createRandomNumber():
import random
myrandomnumber = random.randint(1,21)*5


def viewOne(request):
#code for this view, including needing to generate a random number
createRandomNumber()

def viewTwo(request):
#code for this view, including needing to generate a random number
createRandomNumber()

Thanks very much for any help you can give me

Upvotes: 2

Views: 809

Answers (3)

Mark Bailey
Mark Bailey

Reputation: 1675

Depending on what you are doing with the random number, this logic might be better in a model or manager (E.g. selecting a random instance). That might mean you only need it one place, or you may still want to separate it out into a separate util function.

Upvotes: 0

danny bee
danny bee

Reputation: 870

Just wrap the code you want to reuse in a function and call that function wherever needed.

Also, don't use import statements inside functions!

#views.py
import random
def createRandomNumber():
    myRandomNumber = random.randint(1,21)*5
    return myRandomNumber


def viewOne(request):
#code for this view, including needing to generate a random number
    randomNumber = createRandomNumber()

def viewTwo(request):
#code for this view, including needing to generate a random number
    randomNumber = createRandomNumber()

If you need to use that piece of code in different files use an import statement. So assuming you are working with a file in the same directory as your views.py just add

from .views import createRandomNumber

Upvotes: 0

Rohan Thacker
Rohan Thacker

Reputation: 6337

Well in-order to this you would need to extract the function such that is would be available to all views that need it. You could for example create a file called utils.py in your django app define the function there and import it into the views.py

utils.py

import random

def createRandomNumber():
    return random.randint(1,21)*5

views.py

from utils import createRandomNumber

def viewOne(request):
    createRandomNumber()

def viewTwo(request):
    createRandomNumber()

Upvotes: 1

Related Questions