Jhonny
Jhonny

Reputation: 145

Print a random string generated

I have this code on my views.py

import string, random

def draegg_view(size=40, chars=string.ascii_letters + string.digits + string.punctuation):
    return ''.join(random.choice(chars) for _ in range(size))

My question is how could I print the random string generated since this code?, I can execute this code on python console but i wanna see the result on a page!!

Thanks.

Upvotes: 2

Views: 142

Answers (2)

alecxe
alecxe

Reputation: 474003

Since this is a django view you are talking about, you need to return an HttpResponse object.

In the simplest case, it would be:

import random
import string

from django.http import HttpResponse


def draegg_view(request):
    size = 40
    chars = string.ascii_letters + string.digits + string.punctuation
    result = ''.join(random.choice(chars) for _ in range(size))
    return HttpResponse(result)

Note that a view is one of the key components/concepts in Django, make sure you study Writing Views documentation section and the tutorial carefully.

Upvotes: 5

Joran Beasley
Joran Beasley

Reputation: 114038

import flask

app = flask.Flask()
@app.route("/")
def dreaegg_view(size=40...):
    return ...

app.run()

is probably the easiest way to get it displayed in an html page ...

Upvotes: 4

Related Questions