对不起,我的英语。 静态文件有问题。我有一个多页的教育网站。在主页上,我连接了图片。在第二页上,我无法执行此操作,因为URL的第一行正好对准图片,而第二行则通过另一个URL。
models.py
from django.db import models
class Alex(models.Model): #first page class
title = models.CharField(max_length=300, verbose_name = 'table of contents')
titletwo = models.CharField(max_length=300, null=True, verbose_name = 'Description')
content = models.TextField(null=True, blank=True, verbose_name='url img')
foto = models.ImageField(blank=True, upload_to="images/body/%Y/%m/%d", help_text='150x150px', verbose_name='Ссылка картинки')
class Meta:
verbose_name_plural = 'body'
verbose_name = 'body'
class Bdtwo(models.Model): #for the second page
title = models.CharField(max_length=300, verbose_name='table of contents')
content = models.TextField(null=True, blank=True, verbose_name='Description')
foto = models.ImageField(blank=True, upload_to="images/body/%Y/%m/%d", help_text='150x150px', verbose_name='url img')
class Meta:
verbose_name_plural = 'body2'
verbose_name = 'body2'
url.py-我的项目
from django.contrib import admin
from django.urls import path, include
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
path('blog/', include('blog.urls')),
path('admin/', admin.site.urls),
]
url.py-application
rom django.urls import path
from .views import *
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
path('', index),
path('two', two)
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
settings.py
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'static/media/')
MEDIA_URL = 'static/media/'
views.py
from django.shortcuts import render
from .models import Alex, Bdtwo
def index(request):
bdx = Alex.objects.filter(id=2)
bdc = Alex.objects.filter(id=3)
bdv = Alex.objects.filter(id=4)
context = {'bdx': bdx, 'bdc': bdc, 'bdv': bdv,}
return render(request, 'blog/index.html', context )
def two(request):
bdx = Bdtwo.objects.filter(id=1)
bdc = Bdtwo.objects.filter(id=2)
bdv = Bdtwo.objects.filter(id=3)
context = {'bdx': bdx, 'bdc': bdc, 'bdv': bdv}
return render(request, 'blog/two.html', context)
html-索引
<div class="row featurette">
{% for bb in bdv %}
<div class="col-md-7">
<h2 class="featurette-heading">{{ bb.title }}<span class="text-muted">{{ bb.titletwo }}</span></h2>
<p class="lead">{{ bb.content }}</p>
</div>
<div class="col-md-5">
<img class="featurette-image img-responsive" alt="500x500" src="{{ MEDIA_URL }}{{ bb.foto }}">
</div>
{% endfor %}
</div>
html-两个
<div class="row featurette">
{% for bb in bdv %}
<div class="col-md-7">
<h2 class="featurette-heading">{{ bb.title }}<span class="text-muted">{{ bb.titletwo }}</span></h2>
<p class="lead">{{ bb.content }}</p>
</div>
<div class="col-md-5">
<img class="featurette-image img-responsive" alt="500x500" src="{{ MEDIA_URL }}{{ bb.foto }}">
</div>
{% endfor %}
</div>
如何输入HTML代码是相同的,但这是由于URL重定向链接是以不同的方式进行的。在url.py-application中指定的path(“,index)使其完全沿着路径“ body / static / media / images / body /%Y /%m /%d”运行,但这是正确的路径。但是path( “两个”,两个)位于“ body / two / static / media / images / body /%Y /%m /%d”路径上,此路径不正确。我理解错误,但不知道如何修复它。请帮忙。