我无法在我的模板中加载图像,我尝试更改所有上传文件位置和其他所有内容,并且与类别字段相同
I cannot load the image in my template, I tried changing all upload file location and everything else and its the same with category field
models.py
class product(models.Model):
categoryChoices=[('food','Food'),('clothing','Clothing'),('books','Books'),('furniture','Furniture'),('others','Others')]
product_name=models.CharField(max_length=40)
product_decsription=models.CharField(max_length=100)
pub_date=models.DateTimeField(auto_now=True)
image=models.ImageField(upload_to='CharApp/images/',default='')
address=models.CharField(max_length=200, default='')
city= models.CharField(max_length=40)
station= models.CharField(max_length=40)
category= models.CharField(choices=categoryChoices,max_length=30)
Views.py
def index(request):
donations=product.objects.all()
return render(request,'CharApp/index.html',{'donations':donations})
Urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('',views.start,name='start'),
path('home/',views.index,name='index'),
path('upload/',views.productFormView),
path('about/',views.about,name='about'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Forms.py
class productForm(forms.ModelForm):
class Meta:
model=product
fields='__all__'
widgets= {
'product_name': forms.TextInput(attrs={'class':'form-control'}),
'product_decsription': forms.TextInput(attrs={'class':'form-control'}),
'pub_date': forms.DateInput(attrs={'class':'form-control'}),
'image': forms.FileInput(attrs={'class':'form-control'}),
'address': forms.TextInput(attrs={'class':'form-control'}),
'city': forms.TextInput(attrs={'class':'form-control'}),
'station': forms.TextInput(attrs={'class':'form-control'}),
'category': forms.Select(attrs={'class':'form-control'}),
模板
<table class="table table-bordered">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Description</th>
<th scope="col">Category</th>
<th scope="col">Station</th>
<th scope="col">Image</th>
</tr>
</thead>
{% if donations %}
<tbody>
{% for d in donations %}
<tr>
<th scope="row">1</th>
<td>{{d.product_name}}</td>
<td>{{d.product_description}}</td>
<td>{{d.category}}</td>
<td>{{d.station}}</td>
<td>
<img src="/{{ BASIC_DIR }}/{{d.image}}" width="120"/>
</td>
<td><button type="button" class="btn btn-success">Add</button></td>
</tr>
{% endfor %}
{% else %}
<p>no donations</p>
{% endif %}
我尝试将 img src 调用更改为 d.image.url 调用,还有很多它只是不加载的东西,视图只是不加载这个字段,即使它们在数据库中可用
更改您的模板
{% for d in donations %}
<tr>
<th scope="row">1</th>
<td>{{d.product_name}}</td>
<td>{{d.product_description}}</td>
<td>{{d.get_category_display}}</td>
<td>{{d.station}}</td>
<td>
<img src="{{d.image.url}}" width="120"/>
</td>
<td><button type="button" class="btn btn-success">Add</button></td>
</tr>
{% endfor %}
说明:
对于每个设置了选项的模型字段,Django 将添加一个方法来检索字段当前值的 human-readable 名称。请参阅数据库 API 文档中的 get_FOO_display()。
get_FOO_display
文档:https://docs.djangoproject.com/en/4.0/ref/models/fields/#choices
models.py
class product(models.Model):
categoryChoices=[('food','Food'),('clothing','Clothing'),('books','Books'),('furniture','Furniture'),('others','Others')]
product_name=models.CharField(max_length=40)
product_decsription=models.CharField(max_length=100)
pub_date=models.DateTimeField(auto_now=True)
image=models.ImageField(upload_to='CharApp/images/',default='')
address=models.CharField(max_length=200, default='')
city= models.CharField(max_length=40)
station= models.CharField(max_length=40)
category= models.CharField(choices=categoryChoices,max_length=30)
Views.py
def index(request):
donations=product.objects.all()
return render(request,'CharApp/index.html',{'donations':donations})
Urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('',views.start,name='start'),
path('home/',views.index,name='index'),
path('upload/',views.productFormView),
path('about/',views.about,name='about'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Forms.py
class productForm(forms.ModelForm):
class Meta:
model=product
fields='__all__'
widgets= {
'product_name': forms.TextInput(attrs={'class':'form-control'}),
'product_decsription': forms.TextInput(attrs={'class':'form-control'}),
'pub_date': forms.DateInput(attrs={'class':'form-control'}),
'image': forms.FileInput(attrs={'class':'form-control'}),
'address': forms.TextInput(attrs={'class':'form-control'}),
'city': forms.TextInput(attrs={'class':'form-control'}),
'station': forms.TextInput(attrs={'class':'form-control'}),
'category': forms.Select(attrs={'class':'form-control'}),
模板
<table class="table table-bordered">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Description</th>
<th scope="col">Category</th>
<th scope="col">Station</th>
<th scope="col">Image</th>
</tr>
</thead>
{% if donations %}
<tbody>
{% for d in donations %}
<tr>
<th scope="row">1</th>
<td>{{d.product_name}}</td>
<td>{{d.product_description}}</td>
<td>{{d.category}}</td>
<td>{{d.station}}</td>
<td>
<img src="/{{ BASIC_DIR }}/{{d.image}}" width="120"/>
</td>
<td><button type="button" class="btn btn-success">Add</button></td>
</tr>
{% endfor %}
{% else %}
<p>no donations</p>
{% endif %}
我尝试将 img src 调用更改为 d.image.url 调用,还有很多它只是不加载的东西,视图只是不加载这个字段,即使它们在数据库中可用
更改您的模板
{% for d in donations %}
<tr>
<th scope="row">1</th>
<td>{{d.product_name}}</td>
<td>{{d.product_description}}</td>
<td>{{d.get_category_display}}</td>
<td>{{d.station}}</td>
<td>
<img src="{{d.image.url}}" width="120"/>
</td>
<td><button type="button" class="btn btn-success">Add</button></td>
</tr>
{% endfor %}
说明:
对于每个设置了选项的模型字段,Django 将添加一个方法来检索字段当前值的 human-readable 名称。请参阅数据库 API 文档中的 get_FOO_display()。
get_FOO_display
文档:https://docs.djangoproject.com/en/4.0/ref/models/fields/#choices