Answer the question
In order to leave comments, you need to log in
Why don't models generate random id's?
from django.db import models
from django.utils.crypto import get_random_string
# Create your models here.
class Urls(models.Model):
url = models.URLField()
id = models.TextField(primary_key=True, default=get_random_string())
createdat = models.DateField(auto_now=True)
from django.shortcuts import render
from django.http import HttpResponse
from django.contrib import messages
from .forms import UrlForm
from .models import Urls
# Create your views here.
def index(request):
if request.method == 'GET':
return render(request, 'short.html')
else:
form = UrlForm(request.POST)
if form.is_valid():
# valid form
u = Urls(request.POST.get('URL'))
u.save()
print(u.id)
return render(request, 'takeurlpage.html', {'shortedurl': str(u.id)})
else:
messages.add_message(request, messages.ERROR, 'Invalid URL')
return render(request, 'short.html')
# Show errror message
NQBd7GwfflVa
[08/Apr/2018 06:17:03] "POST / HTTP/1.1" 200 752
NQBd7GwfflVa
[08/Apr/2018 06:17:04] "POST / HTTP/1.1" 200 752
NQBd7GwfflVa
[08/Apr/2018 06:17:05] "POST / HTTP/1.1" 200 752
NQBd7GwfflVa
[08/Apr/2018 06:17:07] "POST / HTTP/1.1" 200 752
NQBd7GwfflVa
[08/Apr/2018 06:17:07] "POST / HTTP/1.1" 200 752
NQBd7GwfflVa
[08/Apr/2018 06:17:08] "POST / HTTP/1.1" 200 752
NQBd7GwfflVa
[08/Apr/2018 06:17:09] "POST / HTTP/1.1" 200 752
[08/Apr/2018 06:17:15] "GET / HTTP/1.1" 200 1403
NQBd7GwfflVa
[08/Apr/2018 06:17:17] "POST / HTTP/1.1" 200 752
[08/Apr/2018 06:20:18] "GET / HTTP/1.1" 200 1403
NQBd7GwfflVa
[08/Apr/2018 06:20:20] "POST / HTTP/1.1" 200 752
[08/Apr/2018 06:20:21] "GET / HTTP/1.1" 200 1403
[08/Apr/2018 06:20:24] "GET / HTTP/1.1" 200 1403
NQBd7GwfflVa
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question