我在第一个django项目中工作。不幸的是,我面临的问题应该是直截了当的。我的情景:
models.py
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.py
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.py
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'
我已通过管理控制台添加对象。所以,我很确定我应该得到结果。我通过很多文件,但我真的不明白我的问题在哪里。感谢您的帮助。
答案 0 :(得分:1)
您正在调用错误的视图。您的urlpattern
应该调用视图list_subj
。
将您的urlpatterns
更改为此 -
urlpatterns = [
url('^subjects/$',poc_views.list_subj,name ='subjects'),
]