我是python&django的新手。我试图创建一个博客(引导模板),并且前4页正在使用我创建的数据库和模型。但是对于第5页,我却无法以某种方式从Django的admin页中创建的对象中提取任何数据。
i删除了迁移并重做了几次。甚至刷新数据库和缓存。看来我的模型只支持4个班级,而没有更多
这是我的 overmij.html ,我在这里尝试像上一页一样提取数据(在这种情况下,是管理员的一些内容),但无法正常工作。
overmij.html(不显示任何内容)
<h1>overmij</h1>
{{info.content}}
exercise.html
<!-- Main Content -->
<div class="container">
<div class="row">
<div class="col-lg-8 col-md-10 mx-auto">
<div class="post-preview">
{% for exercise in exercises %}
<a href="{% url 'web_app:exercise_detail' exercise.id %}">
<h2 class="post-title">
{{exercise.title}}
</h2>
</a>
<p class="post-meta">Posted by
Med
{{exercise.published}}</p>
<hr>
{% endfor %}
</div>
<hr>
<!-- Pager -->
<div class="clearfix">
<a class="btn btn-primary float-left" href="{% url 'web_app:index' %}">← Go to Articles </a>
</div>
</div>
</div>
</div>
ex_details.html `
<h2 class="post-title">
{{exercise.title}}
</h2>
<p class="post-meta">Posted by
Med
{{exercise.published}}</p>
<hr>
{{exercise.content}}
<br>
<br>
<br>
<img src="{{ exercise.image.url }}" height="300">
</div>
`
views.py
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse
from .models import Article, Exercise
from .models import Overmij
def index(request):
return render(request, 'web_app/index.html',{"articles":Article.objects.all})
def detail(request, article_id):
article_detail = get_object_or_404(Article, pk=article_id)
return render(request, 'web_app/article.html', {'article':article_detail})
def exercise(request):
return render(request, 'web_app/exercise.html', {"exercises":Exercise.objects.all})
def ex_details(request, exercise_id):
exercise_detail = get_object_or_404(Exercise, pk=exercise_id)
return render(request, 'web_app/ex_details.html', {'exercise':exercise_detail})
def overmij(request):
return render(request, 'web_app/overmij.html',{'info':Overmij.objects.all})
def web_app_list(request):
return render(request=request,
template_name="web_app/web_app_list.html",
context={"web_app":Article.objects.all})
我的urls.py
from django.urls import path, include
from . import views
from django.conf import settings
from django.conf.urls.static import static
app_name = 'web_app'
urlpatterns = [
path('list',views.web_app_list, name='list'),
path('index',views.index, name='index'),
path('article/<int:article_id>',views.detail, name='detail'),
path('exercise',views.exercise, name='exercise'),
path('exercise_details/<int:exercise_id>',views.ex_details, name='exercise_detail'),
path('overmij',views.overmij, name='overmij'),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
models.py
from django.db import models
from datetime import datetime
# Create your models here.
class Article(models.Model):
title = models.CharField(max_length=200)
content = models.TextField()
published = models.DateTimeField("date published", default= datetime.now())
image = models.ImageField(upload_to='images/', default="")
class Exercise(models.Model):
title = models.CharField(max_length=200)
content = models.TextField()
published = models.DateTimeField("date published", default= datetime.now())
image = models.ImageField(upload_to='images/', default="")
class Overmij(models.Model):
title = models.CharField(max_length=200)
content = models.TextField()
published = models.DateTimeField("date published", default= datetime.now())
image = models.ImageField(upload_to='images/', default="")
def __str__(self):
return self.title
admin.py
from django.contrib import admin
from .models import Article, Exercise, Overmij
from django.db import models
# Register your models here.
class Web_appAdmin(admin.ModelAdmin):
# field are the names of the colums in the models.py
# fields = ["title","published","content"]
# to split it
fieldsets = [
("Title/date", {"fields":["title","published"]}),
("Content", {"fields":["content", "image"]})
]
admin.site.register(Article, Web_appAdmin),
admin.site.register(Exercise, Web_appAdmin),
admin.site.register(Overmij, Web_appAdmin),
settings.py
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'web_app',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'sampleapp.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['/Users/AdamDawdi/djangoApps/sampleapp/web_app/templates/web_app'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'sampleapp.wsgi.application'
# Database
# https://docs.djangoproject.com/en/2.1/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/2.1/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/2.1/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.1/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "web_app/static"),
]
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_cdn")
MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR
```
答案 0 :(得分:1)
与exercise
视图类似,您的overmij
视图在数据库中返回一个 all 元素的查询集。但是在练习模板中,您遍历了查询集以显示每个练习的元素。您不必使用overmij模板来做到这一点。如果要显示每个Overmij对象的内容字段,则需要在模板中进行迭代。
{% for overmij in info %}
{{overmij.content}}
{% endfor %}