Поле 'id' ожидало число, но получило <WSGIRequest: GET '/r/1/'>
я пытался сделать проект, который будет преобразовывать длинный url в короткий. возьмите входной url с html страницы и сохраните его в базе данных и перенаправьте его в длинный url, хотя мой новый url
но получил ошибку
и index.html выглядит как
<h1>short any url here</h1>
<form method="POST">
{% csrf_token %}
<input type="url" name="url" placeholder="enter your url here">
<input type="submit" value="submit">
</form>
{% for urlconverter in urlconv %}
<li><a href="{% url 'shorturl:redirect' urlconverter.id %}">{{ urlconverter.short }} </a></li>
{% endfor %}
urls.py
from django.urls import path
from .import views
app_name='shorturl'
urlpatterns=[
path('shorturl/',views.indx,name='home'),
path('r/<int:urlconverter_id>/',views.redirect,name='redirect'),
]
the models.py
from django.db import models
# Create your models here.
class urlconverter(models.Model):
url=models.CharField(max_length=1000)
short=models.CharField(max_length=100)
def __str__(self):
return self.url
views.py
from django.shortcuts import render,redirect,get_object_or_404
from django.http import HttpResponse
from django.template import loader
from .models import urlconverter
# Create your views here.
def indx(request):
template=loader.get_template('urlconverter/index.html')
urlconv=urlconverter.objects.all()
if request.method=='POST':
a=request.POST.get('url')
b=urlconverter.objects.all()
c=b.count()
nwurl='/r/'+str(c)
s=urlconverter(url=a,short=nwurl)
s.save()
context={
'urlconv':urlconv
}
return HttpResponse(template.render(context,request))
def redirect(request,urlconverter_id):
obj=get_object_or_404(urlconverter,pk=urlconverter_id)
org_url=obj.url
return redirect(org_url,request)
и я получил эту ошибку
TypeError at /r/1/
Field 'id' expected a number but got <WSGIRequest: GET '/r/1/'>.
Request Method: GET
Request URL: http://127.0.0.1:8000/r/1/
Django Version: 4.0.5
Exception Type: TypeError
Exception Value:
Field 'id' expected a number but got <WSGIRequest: GET '/r/1/'>.
может ли кто-нибудь помочь мне, где здесь проблема?