在我的django应用中,我可以使用{{{src="/media/user/image.png"
}}这样的路径apps.upload.url
提供图像,但是现在我想使用类似http://127.0.0.1:8000/media/image.png
的url链接将图像提供给html页面
settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS=(
os.path.join(BASE_DIR, 'static','static_dirs'),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'static','static_root')
MEDIA_ROOT = os.path.join(BASE_DIR, 'static','media')
MEDIA_URL = ('/media/')
models.py
class MyModel(models.Model):
name = models.TextField()
upload = models.ImageField(upload_to=user_directory_path)
urls.py
urlpatterns = [
..............
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
在html页面中:
<img src="/media/user_1/image.png" >
答案 0 :(得分:0)
似乎您正在使用Django Docs中example中的<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow"
Width="600"
Height="500">
<Window.DataContext>
<local:MainWindowViewModel />
</Window.DataContext>
<Window.Resources>
<local:TypeToSubTypesConverter
x:Key="TypeToSubTypesConverter" />
</Window.Resources>
<Grid>
<DataGrid
AutoGenerateColumns="False"
ItemsSource="{Binding Collection}">
<DataGrid.Columns>
<DataGridComboBoxColumn
Header="Type"
SelectedItemBinding="{Binding Type, UpdateSourceTrigger=PropertyChanged}"> <!-- property changed so we get the change right after we select-->
<DataGridComboBoxColumn.ElementStyle>
<Style
TargetType="ComboBox">
<Setter
Property="ItemsSource"
Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.TypeCollection}" />
</Style>
</DataGridComboBoxColumn.ElementStyle>
<DataGridComboBoxColumn.EditingElementStyle>
<Style
TargetType="ComboBox">
<Setter
Property="ItemsSource"
Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.TypeCollection}" />
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>
<DataGridComboBoxColumn
Header="Sub Type"
SelectedItemBinding="{Binding SubType, UpdateSourceTrigger=PropertyChanged}"> <!-- property changed so we get the change right after we select-->
<DataGridComboBoxColumn.ElementStyle>
<Style
TargetType="ComboBox">
<Setter
Property="ItemsSource">
<Setter.Value>
<MultiBinding
Converter="{StaticResource ResourceKey=TypeToSubTypesConverter}">
<Binding
Path="Type" />
<Binding
Path="DataContext.SubTypeCollection"
RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}"/>
</MultiBinding>
</Setter.Value>
</Setter>
</Style>
</DataGridComboBoxColumn.ElementStyle>
<DataGridComboBoxColumn.EditingElementStyle>
<Style
TargetType="ComboBox">
<Setter
Property="ItemsSource">
<Setter.Value>
<MultiBinding
Converter="{StaticResource ResourceKey=TypeToSubTypesConverter}">
<Binding
Path="Type" />
<Binding
Path="DataContext.SubTypeCollection"
RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}" />
</MultiBinding>
</Setter.Value>
</Setter>
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
函数。这会将每个图像保存到基于user_directory_path
的文件夹中。您将需要更新user.id
并删除MyModel
属性:
upload_to
这应该适用于您上传的任何新图像。
您想要的示例还包括一个标准URL而不是一个相对URL。如果您要这样做,请将settings中的class MyModel(models.Model):
name = models.TextField()
upload = models.ImageField()
设置为MEDIA_URL
。
如果系统中已有图像,并且要在路径中没有http://127.0.0.1:8000/media/
的情况下访问它们,则可能需要:
user_x
文件夹移至父文件夹user_x
的值。如果您有很多图像,则可能需要data migration。