问题:模板未随视图一起加载...
\n\n我一直在关注 django 教程,希望能让我创建的一些模板正常工作。看起来像这样。
\n\napp/templates\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 app\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 profile.html\nRun Code Online (Sandbox Code Playgroud)\n\n设置文件如下所示:
\n\n"""\nDjango settings for demonstration project.\n\nGenerated by \'django-admin startproject\' using Django 1.10.3.\n\nFor more information on this file, see\nhttps://docs.djangoproject.com/en/1.10/topics/settings/\n\nFor the full list of settings and their values, see\nhttps://docs.djangoproject.com/en/1.10/ref/settings/\n"""\n\nimport os\n\n# Build paths inside the project like this: os.path.join(BASE_DIR, ...)\nBASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))\n\nTEMPLATE_DIRS = [os.path.join(BASE_DIR, \'templates\')]\n\n# Quick-start development settings - unsuitable for production\n# See https://docs.djangoproject.com/en/1.10/howto/deployment/checklist/\n\n# SECURITY WARNING: keep the secret key used in production secret!\nSECRET_KEY = \'SOME_KEY_NON_PROD_TESTING_LOCALLY\'\n\n# SECURITY WARNING: don\'t run with …Run Code Online (Sandbox Code Playgroud) 我刚刚学习 CBV,并且在将对象传递给 TemplateView 时遇到了困难。这非常令人沮丧,因为我知道这应该是非常基本的。
这是我的观点.py:
from __future__ import absolute_import
from django.views import generic
from company_account.models import CompanyProfile
class CompanyProfileView(generic.TemplateView):
template_name = 'accounts/company.html'
def get_context_data(self, **kwargs):
context = super(CompanyProfileView, self).get_context_data(**kwargs)
return CompanyProfile.objects.all()
Run Code Online (Sandbox Code Playgroud)
这是我的 Models.py:
from __future__ import unicode_literals
from django.db import models
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
class CompanyProfile(models.Model):
company_name = models.CharField(max_length=255)
def __str__(self):
return self.company_name
Run Code Online (Sandbox Code Playgroud)
这是 urls.py
urlpatterns = [
url(r'^accounts/companyprofile/$', CompanyProfileView.as_view()),
]
Run Code Online (Sandbox Code Playgroud)
最后,这是模板:
{% extends '_layouts/base.html' %}
{% block title %}Company Profile{% endblock %}
{% block …Run Code Online (Sandbox Code Playgroud) 我需要在我的主页底部嵌入一个联系表单,这是一个单页应用程序。我遵循了几个关于如何在另一个页面(例如,myportfolio.com/contact)上创建自己的联系表单的教程,这些教程很成功。不幸的是,我无法调整这些结果或找到其他教程,了解如何使该联系表单在显示所有其他模型数据的同一页面上可见并起作用(例如,myportfolio.com)。
我遵循了本教程:https://atsoftware.de/2015/02/django-contact-form-full-tutorial-custom-example-in-django-1-7。我只创建了一个应用程序。
我觉得这应该是一件很常见的事情,这让我觉得我忽略了一些显而易见的事情。
应用程序中的views.py
from django.views.generic import TemplateView
from portfolio.models import Experience
class HomePage(TemplateView):
template_name = 'portfolio/base.html'
def get_context_data(self, *args, **kwargs):
context = super(HomePage, self).get_context_data(*args, **kwargs)
context['experience_list'] = Experience.objects.order_by('-start_date')
return context
Run Code Online (Sandbox Code Playgroud)
contact_form.html
{% extends 'portfolio/base.html' %}
{% block contact %}
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit">
</form>
{% endblock %}
Run Code Online (Sandbox Code Playgroud)
现在 contact_form.html 进入portfolio/base.html,如下所示:
基本.html
{# {% include "contact_form/contact_form.html" %}#}
{% block contact %}{% endblock %}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,什么也没有显示。但是,如果我注释掉第二行并删除第一行中的注释,则会出现提交按钮。
现在我意识到我假设 django-contact-form 将获取此表单并知道如何处理它,就像在 myportflio.com/contact 场景中可以访问它一样。但是,我知道之前曾通过 urls.py 文件引用 …
django django-templates django-forms django-email contact-form
我的 view.py 中有一本字典列表的字典
...
data = {'item':[{'key1':'value' ,'key2':'value2' ,'key3':'value3'}]}
...
Run Code Online (Sandbox Code Playgroud)
在我的模板中,我想获取字典键的值item,
我可以获取字典中的元素item。
但是我如何在模板的 for 循环中获取项目字典元素的键值,
如下所示:
{% for items in item %}
<li>{{item|first}}</li>
{% endfor %}
Run Code Online (Sandbox Code Playgroud) 我正在尝试实现 Twitter 等社交网站上使用的“@”功能,以标记或提及我的 django 项目中的用户。例如,单击“@stack”时应转到堆栈的配置文件。
如何做到这一点对我很有帮助。
在两个不同的模板中,我有两个几乎相同的块,只是 url 不同:
<div class="col-sm-4">
<a href="{% url 'bo:article-list' %}" class="btn btn-block btn-secondary btn-sm"
role="button">
<i class="fa fa-chevron-left"></i> Annuler
</a>
</div>
Run Code Online (Sandbox Code Playgroud)
<div class="col-sm-4">
<a href="{{ article.get_absolute_url }}" class="btn btn-block btn-secondary btn-sm"
role="button">
<i class="fa fa-chevron-left"></i> Annuler
</a>
</div>
Run Code Online (Sandbox Code Playgroud)
我想让这个变得干燥,创建一个模板,然后进行包含。例如:
<div class="col-sm-4">
<a href="{{ cancel_url }}" class="btn btn-block btn-secondary btn-sm"
role="button">
<i class="fa fa-chevron-left"></i> Annuler
</a>
Run Code Online (Sandbox Code Playgroud)
对于template2.html,它将与:
{% include 'includes/_cancel.html' with cancel_url=article.get_absolute_url %}
Run Code Online (Sandbox Code Playgroud)
但是template1.html呢?这显然不起作用:
{% include 'includes/_cancel.html' with cancel_url={% url 'bo:article-list' %}
Run Code Online (Sandbox Code Playgroud)
我想有一个技巧。感谢您的帮助 :)
我创建了一个小型 django 项目。当我尝试重定向到特定页面时,网址不会更改,但所需的页面已正确加载。我正在使用 render() 重定向到特定页面。
视图.py 文件:
def create_album(request):
if not request.user.is_authenticated():
return render(request, 'photo/login.html')
else:
form = AlbumForm(request.POST or None, request.FILES or None)
if form.is_valid():
album = form.save(commit=False)
album.user = request.user
album.album_logo = request.FILES['album_logo']
album.save()
return render(request, 'photo/detail.html', {'album': album})
context = {
"form": form,
}
return render(request, 'photo/create_album.html', context)
def register(request):
form = UserForm(request.POST or None)
if form.is_valid():
user = form.save(commit=False)
username = form.cleaned_data['username']
password = form.cleaned_data['password']
user.set_password(password)
user.save()
user = authenticate(username=username, password=password)
if user is not None: …Run Code Online (Sandbox Code Playgroud) 我有一个取消按钮,我需要在模板中获取上一页的 url,以防用户单击取消返回。
上一页通常是列表或详细信息页面。
在详细信息的情况下,我有对象,因此更容易返回,但是在列表页面的情况下我做什么,以及我如何知道前面是详细信息还是列表页面。
当我在表单中使用引导模式时,它只显示第一个值。
在这里我的template.html
{% for company in companys %}
<tr>
<td>{{ company.name }}</td>
<td>{{ company.desc }}</td>
<td align="center">
<button type="button" class="btn btn-warning margin-bottom" data-toggle="modal" data-target="#modal-default2">
delete
</button>
<div class="modal fade" id="modal-default2">
<div class="modal-dialog">
<form method="post" action="{% url 'system:company_delete' pk=company.pk %}">
{% csrf_token %}
<div class="modal-content">
<div class="modal-body">
<input type="text" name="name" maxlength="100" required="" id="id_name" value="{{ company.pk }}">
<input type="submit" class="btn btn-primary" value="Delete">
</div>
</div>
</form>
</div>
</div>
</td>
</tr>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
它循环所有数据,当点击delete确认表单时会弹出。但它返回相同的值。
但如果没有模态引导它的工作正常。
例子:template.html
{% for company …Run Code Online (Sandbox Code Playgroud) javascript django django-templates django-forms bootstrap-modal
我正在开发一个具有三个选项卡的应用程序,我希望将其中一个选项卡始终标记为活动状态。基于类似的问题,我目前的基本模板中有以下内容:
<div class="tab">
<a class="{% if request.resolver_match.url_name == 'home' %}active{% endif %}" href="/">Home</a>
<a class="{% if request.resolver_match.url_name == 'questions' %}active{% endif %}" href="/questionManager/">Question Manger</a>
<a class="{% if request.resolver_match.url_name == 'course' %}active{% endif %}" href="/courseManager/">Course Manager</a>
</div>
Run Code Online (Sandbox Code Playgroud)
/分别在、/questionManager/和 之间导航时效果很好/courseManager/。问题是,如果我导航到另一个页面(例如/questionManager/addQuestion/),该选项卡将不再标记为活动状态。有没有办法无论我想导航到哪里都可以保持活动状态?具体来说,“问题”选项卡应标记为对所有人有效/questionManager/*,“课程”选项卡应标记为对所有人有效/courseManager/*,而“主页”选项卡应对其他所有内容标记为活动状态。我意识到我可以传递一些变量来指示每当我呈现响应时上下文中是否是问题/课程/主页,但这似乎有很多重复,我觉得有一种更简单的方法可以做我想做的事情。
django ×10
django-templates ×10
python ×5
django-forms ×2
django-views ×2
backend ×1
contact-form ×1
django-email ×1
instagram ×1
javascript ×1