/<user-slug>/<blog-slug> 处的 ValueError 字段 'id' 需要一个数字,但在 django 中得到了 '<user-slug>'
ValueError at /<user-slug>/<blog-slug> Field 'id' expected a number but got '<user-slug>' in django
现在我正在开发一个博客应用程序,它是在不使用 django 管理站点的情况下制作的,我已经添加了博客 post 功能 that.I 已经使用两个外键到一个 User
使用自定义用户模型自定义的模型。
现在,问题是我无法在 row.I 中分配 url 中的两个 slug 已附加视图代码和 urls 以便更好地理解。
我曾尝试删除所有迁移并重新迁移所有内容,但没有成功 me.Is 这个问题有任何解决方案。
这是我的代码,
views.py
from django.shortcuts import render,redirect
from .models import languages,BlogData
from django.conf import settings
from login.models import BaseUserManager,AbstractUser,User
from login.views import register_user
from django.shortcuts import get_object_or_404
from django.utils.text import slugify
# Create your views here.
def index(request):
blogs = BlogData.objects.order_by('-publish_date')
return render(request,"index.html",{'languages':languages, 'blogs':blogs})
def login(request):
return render(request,"login.html")
def about(request,user_slug):
return render(request,"about.html")
def sample(request):
return render(request, "sample.html")
def register(request):
return render(request,"register.html")
"""-------------The main function below from which I am getting error---------"""
def blog_details(request,user_slug,blog_slug):
blog = get_object_or_404(BlogData,user_slug = user_slug,blog_slug = blog_slug)
return render(request,"sample.html",{'blog':blog})
def post(request):
return render(request,"contact.html")
def submit_post(request):
if request.method == 'POST':
blg_tt = request.POST['blog_title']
blg_stt = request.POST['blog_subtitle']
blg_cnt = request.POST['blog_content']
blg_img = request.POST['blog_img']
blg_slg = slugify(blg_tt)
print(User.user_slug)
data = BlogData.objects.create(blogger_name=request.user,blog_title=blg_tt,blog_subtitle=blg_stt,blog_body=blg_cnt,blog_baseimg=blg_img,blog_slug=blg_slg,user_slug=request.user)
data.save()
return redirect("index")
urls.py
from django.urls import path,include
from . import views
urlpatterns = [
path("",views.index, name = "index"),
path("login/",views.login, name = "login"),
path("about",views.about,name = "about"),
path("sample",views.sample, name = "sample"),
path("register",views.register,name = "register"),
path("contact",views.post,name = "post"),
path("post_blog",views.submit_post,name = "post_blog"),
path("<slug:user_slug>/",views.about,name = "about"),
# Here's the path below from I want to call the view function
path("<slug:user_slug>/<slug:blog_slug>/",views.blog_details,name = "blog_details"),
]
models.py
from django.db import models
from django.conf import settings
from django.urls import reverse
from login.models import User
from django.utils.text import slugify
# Create your models here.
languages = ['eng','fra','spa','hin']
class BlogData(models.Model):
blogger_name = models.ForeignKey(User,on_delete=models.CASCADE,related_name='blogger_name')
publish_date = models.DateTimeField(auto_now=True)
blog_title = models.TextField()
blog_subtitle = models.TextField()
blog_body = models.TextField()
blog_baseimg = models.ImageField(upload_to='pics',null=True)
blog_slug = models.SlugField(max_length=200,db_index=True,unique=False)
user_slug = models.ForeignKey(User,on_delete=models.CASCADE,related_name='users_slug')
def get_absolute_url(self):
#Here I had given the views function args
return reverse('blog_details',args=[self.user_slug.user_slug,self.blog_slug])
def __str__(self):
return self.blog_title
models.py(自定义用户模型)
from django.db import models
from django.contrib.auth.models import AbstractUser,BaseUserManager
# Create your models here.
class UserManager(BaseUserManager):
use_in_migrations = True
def _create_user(self,email,password,**kwargs):
email = self.normalize_email(email=email)
user = self.model(email=email,**kwargs)
user.set_password(password)
user.save(using = self._db)
return user
def create_user(self,email,password = None,**kwargs):
kwargs.setdefault('is_staff',False)
kwargs.setdefault('is_superuser',False)
return self._create_user(email,password,**kwargs)
class User(AbstractUser):
username = None
email = models.EmailField(unique = True)
user_slug = models.SlugField(max_length=200,unique=False)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
objects = UserManager()
试试这个:
def blog_details(request,user_slug,blog_slug):
blog = get_object_or_404(BlogData, user_slug__user_slug=user_slug, blog_slug=blog_slug)
return render(request,"sample.html",{'blog':blog})
现在我正在开发一个博客应用程序,它是在不使用 django 管理站点的情况下制作的,我已经添加了博客 post 功能 that.I 已经使用两个外键到一个 User
使用自定义用户模型自定义的模型。
现在,问题是我无法在 row.I 中分配 url 中的两个 slug 已附加视图代码和 urls 以便更好地理解。
我曾尝试删除所有迁移并重新迁移所有内容,但没有成功 me.Is 这个问题有任何解决方案。
这是我的代码,
views.py
from django.shortcuts import render,redirect
from .models import languages,BlogData
from django.conf import settings
from login.models import BaseUserManager,AbstractUser,User
from login.views import register_user
from django.shortcuts import get_object_or_404
from django.utils.text import slugify
# Create your views here.
def index(request):
blogs = BlogData.objects.order_by('-publish_date')
return render(request,"index.html",{'languages':languages, 'blogs':blogs})
def login(request):
return render(request,"login.html")
def about(request,user_slug):
return render(request,"about.html")
def sample(request):
return render(request, "sample.html")
def register(request):
return render(request,"register.html")
"""-------------The main function below from which I am getting error---------"""
def blog_details(request,user_slug,blog_slug):
blog = get_object_or_404(BlogData,user_slug = user_slug,blog_slug = blog_slug)
return render(request,"sample.html",{'blog':blog})
def post(request):
return render(request,"contact.html")
def submit_post(request):
if request.method == 'POST':
blg_tt = request.POST['blog_title']
blg_stt = request.POST['blog_subtitle']
blg_cnt = request.POST['blog_content']
blg_img = request.POST['blog_img']
blg_slg = slugify(blg_tt)
print(User.user_slug)
data = BlogData.objects.create(blogger_name=request.user,blog_title=blg_tt,blog_subtitle=blg_stt,blog_body=blg_cnt,blog_baseimg=blg_img,blog_slug=blg_slg,user_slug=request.user)
data.save()
return redirect("index")
urls.py
from django.urls import path,include
from . import views
urlpatterns = [
path("",views.index, name = "index"),
path("login/",views.login, name = "login"),
path("about",views.about,name = "about"),
path("sample",views.sample, name = "sample"),
path("register",views.register,name = "register"),
path("contact",views.post,name = "post"),
path("post_blog",views.submit_post,name = "post_blog"),
path("<slug:user_slug>/",views.about,name = "about"),
# Here's the path below from I want to call the view function
path("<slug:user_slug>/<slug:blog_slug>/",views.blog_details,name = "blog_details"),
]
models.py
from django.db import models
from django.conf import settings
from django.urls import reverse
from login.models import User
from django.utils.text import slugify
# Create your models here.
languages = ['eng','fra','spa','hin']
class BlogData(models.Model):
blogger_name = models.ForeignKey(User,on_delete=models.CASCADE,related_name='blogger_name')
publish_date = models.DateTimeField(auto_now=True)
blog_title = models.TextField()
blog_subtitle = models.TextField()
blog_body = models.TextField()
blog_baseimg = models.ImageField(upload_to='pics',null=True)
blog_slug = models.SlugField(max_length=200,db_index=True,unique=False)
user_slug = models.ForeignKey(User,on_delete=models.CASCADE,related_name='users_slug')
def get_absolute_url(self):
#Here I had given the views function args
return reverse('blog_details',args=[self.user_slug.user_slug,self.blog_slug])
def __str__(self):
return self.blog_title
models.py(自定义用户模型)
from django.db import models
from django.contrib.auth.models import AbstractUser,BaseUserManager
# Create your models here.
class UserManager(BaseUserManager):
use_in_migrations = True
def _create_user(self,email,password,**kwargs):
email = self.normalize_email(email=email)
user = self.model(email=email,**kwargs)
user.set_password(password)
user.save(using = self._db)
return user
def create_user(self,email,password = None,**kwargs):
kwargs.setdefault('is_staff',False)
kwargs.setdefault('is_superuser',False)
return self._create_user(email,password,**kwargs)
class User(AbstractUser):
username = None
email = models.EmailField(unique = True)
user_slug = models.SlugField(max_length=200,unique=False)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
objects = UserManager()
试试这个:
def blog_details(request,user_slug,blog_slug):
blog = get_object_or_404(BlogData, user_slug__user_slug=user_slug, blog_slug=blog_slug)
return render(request,"sample.html",{'blog':blog})