Django创建表单问题

时间:2017-09-07 09:40:30

标签: django django-models django-forms django-templates

我有一个简单的问题,我真的不知道如何解决。 我正在使用model_form.html创建表单。我的html以这种方式位于模板文件夹中:App_name> templates> model_form.html

问题是django正在查找App_name> model_form.html,不包括模板文件夹。我收到了错误消息:

raise TemplateDoesNotExist(', '.join(template_name_list), chain=chain)
django.template.exceptions.TemplateDoesNotExist: website/project_form.html

views.py:

from django.shortcuts import render
from django.views import generic
from django.views.generic import TemplateView
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from .forms import InviteForm
from invitations.models import Invitation
from .models import project

class ProjectCreate(CreateView):
    model = project
    fields = ['project_name']

url.py:

from django.conf.urls import url
from website import views

app_name = 'website'
urlpatterns = [
    url(r'^candidateIndex/$', views.CandidateIndex.as_view(), name='candidate_index'),
    url(r'^HRIndex/$', views.create_invite, name='HR_index'),
    url(r'^(?P<pk>[0-9]+)/$',views.DetailView.as_view(), name='ProjectDetails'),
    url(r'^project/add/$',views.ProjectCreate.as_view(), name='addproject')
]

我的HtML:

{% extends 'base.html' %}

{% block body %}
<div class="jumbotron">
  <h1>Welcome to SoftScores.com</h1>
  <h2>Team analytics platfom</h2>
  <h3>Welcome to {{User.username}}, it is your Page</h3>
</div>
<div class="container">
  <div class="">
    Create a new project :<a href="{% url 'website:addproject' %}"><span class="glyphicon glyphicon-plus"></span></a>
  </div>
  <p>
    <a class="btn btn-primary" data-toggle="collapse" href="#collapseExample" aria-expanded="false" aria-controls="collapseExample">
      Create a new team
    </a>
  </p>
  <div class="collapse" id="collapseExample">
    <div class="card card-body">
      In order to create a new team please invite new members. A link will be sent to them in order to give the access to the application
    </div>
    <form method="post">
    {% csrf_token %}
    {{ invite_form.as_p }}
    <input type="submit" value="Submit" />
    </form>
  </div>

</div>



{% endblock %}

你能帮我链接吗?thx

2 个答案:

答案 0 :(得分:1)

如果您将模板放在应用程序中,则文件夹结构必须为protocol Node: AnyObject { } protocol Parent: Node { associatedtype ChildNodeType: Node var children: [ChildNodeType] { get set } } protocol Child: Node { associatedtype ParentNodeType: Node weak var parent: ParentNodeType? { get set } } protocol UnarchivableChild: Child { init(dictionary: [String: Any]) // Removed parent parameter! } protocol UnarchivingParent: Parent { associatedtype ChildNodeType: UnarchivableChild func readChildren(fromDictionaries dictionaries: [[String: Any]]) -> [ChildNodeType] } extension UnarchivingParent { func readChildren(fromDictionaries dictionaries: [[String: Any]]) -> [ChildNodeType] { return dictionaries.flatMap({ let child = ChildNodeType(dictionary: $0) // Assign parent here instead: child.parent = self // < ERROR HERE return child }) } } 。这通常用于独立的可重用应用程序。

更常见的是,您可以将它们放在项目级模板文件夹中,您只需要project_name > app_name > templates > app_name > template_name.html

答案 1 :(得分:0)

Django期望在app的模板目录的子目录中查找模板:

var arr1 = [5686, 9890];
var arr2 = ['Staff Count', 'Student Count'];

var result = arr1.map((v, index) => [arr2[index], v]);

console.log(result);

听起来你现在有模板:

websites/templates/website/project_form.html

Yo可以移动模板,也可以为您的视图明确设置websites/templates/project_form.html

template_name