我是Django的新手。今天,我决定不遵循任何教程,而不要做自己的网站。
我的models.py
在这里
class Portfolio(models.Model):
title = models.CharField(max_length=100)
description = models.TextField(max_length=1000)
technology = models.CharField(max_length=100)
img = models.ImageField(upload_to='media/')
site = models.URLField(max_length=200)
def __str__(self):
return self.title
class Meta:
verbose_name = "project"
verbose_name_plural = "projects"
我的views.py
是
def works(request):
work = Post.objects.all().order_by()
context = {'work': work}
return render(request, 'portfolio.html', context)
模板是
<h3 class="text-center card-title title font-weight-bolder">{{how.do.i.put.it}}</h3>
{{here}}
里面的单词在哪里?
我希望我解释得很好。
答案 0 :(得分:0)
返回渲染(请求,“ portfolio.html”,context = {“ work”:work})
检查是否可行
{{work}}
检查是否有名为post的模型,以及是否已在admin中注册并将其导入到视图中
答案 1 :(得分:0)
您是否有名为Post的模型?如果是这样,则尝试使用此:
def works(request):
works = Post.objects.all()
context = {'works': works}
return render(request, 'portfolio.html', context)
在模板中尝试
{% for work in works %}
<h3 class="text-center card-title title font-weight-bolder">{{work.title}}</h3>
{% endfor %}
对于图像,请在项目的url.py中添加
from django.conf.urls.static import static
urlpatterns = [
........
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
settings.py
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
在views.py中添加request.FILES以添加图片字段
form = AddPortfolio(request.POST or None,request.FILES or None)
在模板中添加
enctype = "multipart/form-data"
<form action="" method="post" enctype = "multipart/form-data">
并像这样访问模板中的图像
{% for work in works %}
<img src = /media/{{work.img}} width="" height="">
{% endfor %}