Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
DJANGO将静态文件与路径引用结合使用_Django - Fatal编程技术网

DJANGO将静态文件与路径引用结合使用

DJANGO将静态文件与路径引用结合使用,django,Django,我之前发布了类似的内容,但没有找到合适的解决方案。我正在努力解决的问题之一是在DJANGO html模板中提供静态路径/文件引用的能力。希望通过发布另一个问题,我能够理解这是如何工作的。做了相当多的研究,并通读了DJANGO文档,但没有找到与我的场景相关的内容 我们开始: 在我的模型中,我使用路径引用字段 class Product_images(models.Model): product = models.ForeignKey(Products, on_delete=models.SET_N

我之前发布了类似的内容,但没有找到合适的解决方案。我正在努力解决的问题之一是在DJANGO html模板中提供静态路径/文件引用的能力。希望通过发布另一个问题,我能够理解这是如何工作的。做了相当多的研究,并通读了DJANGO文档,但没有找到与我的场景相关的内容

我们开始:

在我的模型中,我使用路径引用字段

class Product_images(models.Model):
product = models.ForeignKey(Products, on_delete=models.SET_NULL, blank=True, null=True)
path_to_image = models.CharField(max_length=150,null=True, blank=True)
name = models.CharField(max_length=50,unique=False,blank=True)

class Meta:
    verbose_name = 'Product Image'
    verbose_name_plural = 'Product Images' 

def __str__(self):
    return '{} - {} - {}'.format(self.pk, self.product, self.name)
此字段的值设置为(示例): 静态\images\Product\PowerBI\recordinates\recordinates 6.png

这些文件物理存储在app Main/static/…中

我的设置文件包含:

    STATIC_URL = '/static/'
    STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
    MEDIA_ROOT = os.path.join(BASE_DIR, 'Main/')
    MEDIA_URL = '/Main/'
然后我在应用程序中有两个模板,我想在其中提供这些图像。一个页面以以下方式使用自定义上下文处理器:

{{ product_section }}
它返回html,包括:

html_value += u'<img class="d-block w-100" src="{}" width="400px" height="250x" alt="{}">'.format(productimages_obj.path_to_image,productimages_obj.name)
html\u value+=u'。格式(productimages\u obj.path\u to\u image,productimages\u obj.name)
此上下文处理器标记在products_view函数返回的模板中使用

现在,我想在另一个view gallery_视图中使用相同的图像:

def gallery_view(request, requestid, *arg, **kwargs):

productimages = Product_images.objects.filter(product=requestid)
if productimages.exists() != True:
    return HttpResponseNotFound('<h1>Page not found</h1>')

context = {
    'productimages': productimages
}

return render(request, "gallery.html", context)
def gallery_视图(请求、请求ID、*arg、**kwargs):
productimages=Product\u images.objects.filter(Product=requestid)
如果productimages.exists()!=正确:
返回HttpResponseNotFound('未找到页面')
上下文={
“productimages”:productimages
}
返回渲染(请求“gallery.html”,上下文)
使用以下模板标记{{productimages.path_to_image}时,我得到404“GET/Gallery/static/images/Product/PowerBI/Finance/Finance%207.png HTTP/1.1”404 3485

模板编码如下:

<section id="gallery" class="bg-light">
<div class="container-fluid">
    <div class="row">
        {% for productimages in productimages %}               
            <div class="col-md">
                <img src="{{ productimages.path_to_image }}" onclick="openModal();currentSlide({{ forloop.counter }})" class="hover-shadow">                   
            </div>          
        {% endfor %}
    </div>
</div>

{%productimages%中的productimages%}
{%endfor%}
最后但并非最不重要的URL.py:

urlpatterns = [
path('', views.home_view, name='home'),
path('Home', views.home_view, name='home'),
path('PowerBI', views.products_view, name='power bi'),
path('Services', views.services_view, name='services'),
path('About', views.about_view, name='about'),
path('Contact', views.contact_view, name='contact'),
path('Gallery/<int:requestid>', views.gallery_view, name='gallery'),
urlpatterns=[
路径(“”,views.home_view,name='home'),
路径('Home',views.Home_view,name='Home'),
路径('PowerBI',views.products_view,name='PowerBI'),
路径('Services',views.Services_view,name='Services'),
路径('About',views.About_view,name='About'),
路径('Contact',views.Contact_view,name='Contact'),
路径('Gallery/',views.Gallery_view,name='Gallery'),
]+静态(settings.MEDIA\u URL,document\u root=settings.MEDIA\u root)

我在这里犯了什么错误?为什么GET在URL中包含/Gallery/?我该如何避免这种情况


谢谢大家。

我将设置一个图像字段,并在模板中使用其URL属性:

models.py:

image = models.ImageField(null=True, blank=True)
.html文件:

<img src="{{object.image.url}}" alt="" class="img-fluid">


不幸的是,这对我不起作用,因为我无法在自定义上下文处理器中实现。这是因为我无法生成带有模板标记的html代码。实际上,我重新编码了我的解决方案,并成功地在我的项目中使用了图像字段。