Задать вопрос
@marataziat
Джангист-тракторист

Почему модели не генерируют рандомные id?

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
  • Вопрос задан
  • 109 просмотров
Подписаться 1 Простой 2 комментария
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы