Python 向Django添加视图
我正在尝试将django教程应用程序转换为我自己的应用程序,并且需要添加一些不同的视图。我已经在我的数据库中添加了一个名为“Owner”的新列,并对其进行了迁移,现在正在尝试创建一个新的视图来显示所有者数据(类似于教程中显示问题及其数据的方式)。你知道我错过了哪些步骤来克服这个错误吗 预期的输出将是打开一个基于stakeholdes.html文件的功能页面:Python 向Django添加视图,python,django,Python,Django,我正在尝试将django教程应用程序转换为我自己的应用程序,并且需要添加一些不同的视图。我已经在我的数据库中添加了一个名为“Owner”的新列,并对其进行了迁移,现在正在尝试创建一个新的视图来显示所有者数据(类似于教程中显示问题及其数据的方式)。你知道我错过了哪些步骤来克服这个错误吗 预期的输出将是打开一个基于stakeholdes.html文件的功能页面: http://127.0.0.1:7000/polls/stakeholders url.py from django.urls im
http://127.0.0.1:7000/polls/stakeholders
url.py
from django.urls import path
from django.conf.urls import include
from . import views
app_name = 'polls'
urlpatterns = [
path('', views.IndexView.as_view(), name='index'),
path('<int:pk>/stakeholders/', views.StakeholdersView, name='stakeholders'),
path('<int:pk>/', views.DetailView.as_view(), name='detail'),
path('<int:pk>/results/', views.ResultsView.as_view(), name='results'),
path('<int:question_id>/vote/', views.vote, name='vote'),
path('django_plotly_dash/', include('django_plotly_dash.urls')),
]
forms.py
from django import forms
from .models import Question
class QuestionForm(forms.ModelForm):
class Meta:
model = Question
fields = "__all__"
models.py
from django.db import models
from django.utils import timezone
import datetime
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
def __str__(self):
return self.question_text
def was_published_recently(self):
now = timezone.now()
return now - datetime.timedelta(days=1) <= self.pub_date <= now
was_published_recently.admin_order_field = 'pub_date'
was_published_recently.boolean = True
was_published_recently.short_description = 'Published recently?'
class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)# Create your models here.
def __str__(self):
return self.choice_text
class Owner(models.Model):
owner_text = models.CharField(max_length=200)
def __str__(self):
return self.owner_text
来自django.db导入模型的
从django.utils导入时区
导入日期时间
课堂提问(models.Model):
问题文本=models.CharField(最大长度=200)
发布日期=models.datetime字段(“发布日期”)
定义(自我):
返回self.question\u文本
def最近发布(自我):
now=时区。now()
立即返回-datetime.timedelta(days=1)问题出在您的url路径上
现在,您的URL.py如下所示:
from django.urls import path
from django.conf.urls import include
from . import views
app_name = 'polls'
urlpatterns = [
path('', views.IndexView.as_view(), name='index'),
path('<int:pk>/stakeholders/', views.StakeholdersView, name='stakeholders'),
path('<int:pk>/', views.DetailView.as_view(), name='detail'),
path('<int:pk>/results/', views.ResultsView.as_view(), name='results'),
path('<int:question_id>/vote/', views.vote, name='vote'),
path('django_plotly_dash/', include('django_plotly_dash.urls')),
]
从django.url导入路径
从django.conf.url导入包括
从…起导入视图
应用程序名称='polls'
URL模式=[
路径(“”,views.IndexView.as_view(),name='index'),
路径(“/perioders/”,views.StakeholdersView,name='perioders'),
路径('/',views.DetailView.as_view(),name='detail'),
路径('/results/',views.ResultsView.as_view(),name='results'),
路径('/vote/',views.vote,name='vote'),
路径('django\u plotly\u dash/',包括('django\u plotly\u dash.url'),
]
它应该有一些路径,包括“polls/涉众/”,我已经在您的第二条路径中包括了它:
from django.urls import path
from django.conf.urls import include
from . import views
app_name = 'polls'
urlpatterns = [
path('', views.IndexView.as_view(), name='index'),
path('polls/stakeholders/', views.StakeholdersView, name = 'stakeholders'),
path('<int:pk>/', views.DetailView.as_view(), name='detail'),
path('<int:pk>/results/', views.ResultsView.as_view(), name='results'),
path('<int:question_id>/vote/', views.vote, name='vote'),
path('django_plotly_dash/', include('django_plotly_dash.urls')),
]
从django.url导入路径
从django.conf.url导入包括
从…起导入视图
应用程序名称='polls'
URL模式=[
路径(“”,views.IndexView.as_view(),name='index'),
路径('polls/涉众/',views.stateHoldersView,name='涉众'),
路径('/',views.DetailView.as_view(),name='detail'),
路径('/results/',views.ResultsView.as_view(),name='results'),
路径('/vote/',views.vote,name='vote'),
路径('django\u plotly\u dash/',包括('django\u plotly\u dash.url'),
]
您没有路径匹配轮询/涉众。。。。您所期望的最接近的整数如下所示:
from django.urls import path
from django.conf.urls import include
from . import views
app_name = 'polls'
urlpatterns = [
path('', views.IndexView.as_view(), name='index'),
path('polls/stakeholders/', views.StakeholdersView, name = 'stakeholders'),
path('<int:pk>/', views.DetailView.as_view(), name='detail'),
path('<int:pk>/results/', views.ResultsView.as_view(), name='results'),
path('<int:question_id>/vote/', views.vote, name='vote'),
path('django_plotly_dash/', include('django_plotly_dash.urls')),
]