这是我的模型
from django.db import models
from django.contrib.auth.models import User
from django.conf import settings
from django.urls import reverse
class Escort(models.Model):
name = models.CharField(max_length=40)
author = models.ForeignKey(User, on_delete=models.CASCADE)
bio = models.TextField()
phone = models.CharField(max_length=14)
ethnicity = models.CharField(max_length=20)
orientation = models.CharField(max_length=20)
location = models.CharField(max_length=40)
area = models.TextField()
skin_color = models.CharField(max_length=40)
hair_color = models.CharField(max_length=40)
services = models.CharField(max_length=255)
paid = models.BooleanField(default=False)
def __str__(self):
return self.name + '~' + str(self.author)
def get_absolute_url(self):
return reverse('escort-details', args=(str(self.id)))
这些是我的观点
from django.shortcuts import render
from django.views.generic import ListView, DetailView, CreateView
from .models import Escort
#def home(request):
#return render(request, 'home.html',{})
class HomeView(ListView):
model = Escort
template_name = 'home.html'
class EscortDetailView(DetailView):
model = Escort
template_name = 'escort_details.html'
class AddEscortView(CreateView):
model = Escort
template_name = 'add_escort.html'
fields = '__all__'
这些是我的网址
from django.urls import path
from . import views
from .views import HomeView, EscortDetailView, AddEscortView
urlpatterns = [
#path('', views.home, name="home"),
path('', HomeView.as_view(), name="home"),
path('escort/<int:pk>', EscortDetailView.as_view(), name="escort-details"),
path('postad/', AddEscortView.as_view(), name="add-escort"),
]
对于四个视图我也有四个模板
我主要做的是创建一个网站来宣传护送服务。我希望用户注册/登录并单击链接“PostAd”并重定向到带有护送表单的页面。在为护送(护送模型)填写所有信息后,用户将需要先付款,然后广告/帖子才会出现在主页上(护送的 ul 将在主页中使用 HTML 进行组织)。谁能从这里给我建议?
答案 0 :(得分:0)
如果您希望用户拥有帐户并登录,则需要 authentication。如果您想限制用户可以看到和执行的操作,您需要权限。 Django 为此提供了一个内置系统。
这是一个示例教程:Django Tutorial Part 8: User authentication and permissions
请注意,默认情况下 Django 的权限系统仅处理模型级别的权限,即。 e.例如,可以授予用户编辑模型类型的权限。如果您想拥有模型实例的权限,则需要查看对象级权限。