我在我的第一个django项目中工作。不幸的是,我面临的问题应该是直截了当的。我的情景:
models.朋友
from django.db import models
# Create your models here.
## 1. Model for user registration
class subjects(models.Model):
description = models.CharField(max_length=500)
def __Str__(self):
return self.description
views.朋友
from django.contrib.auth import login , authenticate
from django.shortcuts import render , redirect
from poc_teaching.models import subjects
# Create your views here.
def list_subj(request):
subj = subjects.objects.all()
return render (request,'subjects.html',{'subjects': subj})
URLs.朋友
from django.conf.urls import url
#from . import views
from poc_teaching import views as poc_views
urlpatterns = [
url('subjects/$',poc_views.subjects,name ='subjects'),
]
HTML
<head>
"hello world"
</head>
<div>
{{ subjects }}
</div>
我收到了错误:'subjects' object has no attribute 'get'
我已通过管理控制台添加对象。所以,我很确定我应该得到结果。我通过很多文件,但我真的不明白我的问题在哪里。我感谢您的帮助。
你正在调用错误的观点。你的urlpattern
应该调用视图list_subj
。
把你的urlpatterns
改成这个 -
urlpatterns = [
url('^subjects/$',poc_views.list_subj,name ='subjects'),
]