0

现在我正在开发一个博客应用程序,该应用程序是在不使用 django 管理站点的情况下制作的,并且我已经添加了博客文章功能。我已经使用了两个外键到User使用自定义用户模型定制的单个模型中。

现在,问题是我不能在一个 url 中连续分配两个 slug。我附上了视图和 url 的代码以便更好地理解。

我曾尝试删除所有迁移并重新迁移所有内容,但它对我没有用。这个问题有什么解决方案吗?

这是我的代码,

视图.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")

网址.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"),
]

模型.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()
4

1 回答 1

0

尝试这个:

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})
于 2020-08-01T07:23:55.340 回答