我有一个使用URL添加/编辑/查看记录到数据库的站点。这些将信息传递到另一个模板以进行数据输入。我现在正在研究模式形式-目前我们使用的是CSS,而不是Django模式形式。
我的基本问题是,是否可以保留网址和模板,但将其嵌入到模式中?这样,我们可以保留相同的代码并迁移到模式,而无需重新编码视图。
我正在为此寻找一个清晰的简单文档-因此,请给我发送任何好的链接。
对于一个基本示例(我们有更复杂的视图):
模板存储
{% extends 'storage/base.html' %}
{% load staticfiles %}
{% load widget_tweaks %}
{% block content%}
{% include "mainheader.html" %}
{% include "storage/submenu.html" %}
<div class="container top">
<div class="row">
{% for storage in storage.all %}
<a class="text-light" href="{% url 'depot:detailstorage' storage.store_id %}">
<div class="col-md-4">
<div class="card text-white bg-dark mb-3" style="max-width: 18rem;">
<!--div class="card-header">{{ storage.store_name }}</div-->
<div class="card-body" style="">
<h5 class="card-title text-center">{{ storage.store_name }}</h5>
<img class="card-img-top p-3" src="{{ storage.icon_desc.icon.url }}" /></a>
<p class="card-text" style = "font-size:0.7em;">{{ storage.address_1 }} {{ storage.address_2 }} {{ storage.city }} {{ storage.region }} {{ storage.zip }} {{ storage.country }}</p>
</div>
</div>
</div>
</a>
{% endfor %}
</div>
</div>
{% endblock %}
模板创建存储
{% extends 'storage/base.html' %}
{% load widget_tweaks %}
{% block content %}
{% include "mainheader.html" %}
{% include "storage/submenu.html" %}
<div class="container top">
<h4 class="kap">Storage</h4>
<div class="col-sm-3">
<form method="POST" class="post-form">{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="save btn btn-dark mt-3t">Save</button>
</form>
</div>
</div>
{% endblock %}
意见 存储
def allstorage(request):
allstorage = Storage.objects.all()
return render(request, 'storage/allstorage.html',
{
'storage':allstorage
})
create_storage
def createstorage(request):
if request.method == "POST":
form = StorageForm(request.POST)
if form.is_valid():
post = form.save(commit=False)
post.created_by = request.user
# post.modified_by = request.user
# post.datetime = datetime.datetime.now()
post.save()
return redirect('depot:allstorage')
else:
form = StorageForm()
return render(request, 'storage/create_storage.html', {'form': form})
网址
re_path(r'^storage/$', allstorage, name='allstorage'),
url(r'^storage/createstorage/$', createstorage, name='createstorage'),
(是的,我们还需要将所有URL更新为path / re_path)