Django-CMS - Django 模型表单不呈现

Django-CMS - Django Model Form not rendering

我正在使用 Django-CMS 在 Django 中构建一个新站点,它需要一个表单来过滤 JSON 结果和 return 过滤集。

我的问题是,最初,我什至无法让 Django 模型表单呈现,但我可以让 CSRF 令牌工作,所以表单在技术上是呈现的,但 inputs/fields 不是完全出现了。

models.py:

from django.db import models

from .jobs.jobs import *

roles = get_filters()

loc = roles[0]
j_type = roles[1]
industry = roles[2]

class Filter(models.Model):
    country = models.CharField(max_length=255)
    job_type = models.CharField(max_length=255)  
    industry = models.CharField(max_length=255)
    search = models.CharField(max_length=255)

jobs/jobs.py

try:
    from BeautifulSoup import BeautifulSoup
except ImportError:
    from bs4 import BeautifulSoup

import urllib3 
import json 

http = urllib3.PoolManager()

def get_filters():

    response = http.request('GET', 'http://206.189.27.188/eo/api/v1.0/jobs')

    jobs = json.loads(response.data.decode('UTF-8'))

    job_list = []

    for job, values in jobs.items():
        job_list.append(values)

    roles_data = []

    for job in job_list[0]:
        roles_data.append(job)

    roles_data = sorted(roles_data, key=lambda role : role["id"], reverse=True)

    loc = []
    j_type = []
    industry = []

    for role in roles_data:
        loc.append(role['location'])
        j_type.append(role['type'])
        industry.append(role['industry'])

    return loc, j_type, industry

views.py

from django.shortcuts import render
from django.http import HttpResponseRedirect

from .forms import FilterForm

def index(request):
    form = FilterForm()
    context = {
        'form': form,
    } 
    return render(request, 'blog.html', context)

forms.py

from django.forms import ModelForm

from .models import Filter

class FilterForm(ModelForm):
    class Meta:
        model = Filter
        fields = ['country', 'job_type', 'industry', 'search']

urls.py

from __future__ import absolute_import, print_function, unicode_literals

from cms.sitemaps import CMSSitemap
from django.conf import settings
from django.conf.urls import include, url
from django.conf.urls.i18n import i18n_patterns
from django.contrib import admin
from django.contrib.sitemaps.views import sitemap
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.views.static import serve

from . import views

admin.autodiscover()

urlpatterns = [
    url(r'^sitemap\.xml$', sitemap,
        {'sitemaps': {'cmspages': CMSSitemap}}),
    url(r'^accounts/', admin.site.urls),
    url(r'^services/latest-roles/', views.filter_form, name="filter_form" )
]

urlpatterns += i18n_patterns(
    url(r'^admin/', include(admin.site.urls)),  # NOQA
    url(r'^', include('cms.urls')),
)

# This is only needed when using runserver.
if settings.DEBUG:
    urlpatterns = [
        url(r'^media/(?P<path>.*)$', serve,
            {'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
        ] + staticfiles_urlpatterns() + urlpatterns

blog.html

{% extends 'includes/templates/layout.html' %}
{% load cms_tags i18n sekizai_tags zinnia %}

{% block main_content %}
    {% include 'components-header-image.html' %}

    <section class="module">
            <div class="container">
                <div class="row">
                        <div class="col-md-12 m-auto text-center">
                            <form action="POST" class="col-md-12">
                                {% csrf_token %}
                                {{ form.as_p }}
                                <button name="submit" class="btn btn-brand">Search</button>
                            </form>
                            {% get_all_roles%}
                        </div>
                            <div class="col-md-12">
                                <div class="space" data-MY="120px"></div>
                            </div>
                        <div class="col-md-12 flex-center text-center">
                            {% placeholder 'jobs-bottom-statement' %} 
                        </div>
                    </div>
            </div>
    </section>


          <!-- Image-->
          {% include 'components-contact.html' %}

          <!-- Image end-->
{% endblock %}

抱歉提供了大量信息,只是想包含任何人可能需要的任何信息。

正如我所说,CSRF 令牌正在显示,没有任何地方抛出错误,只是根本不显示字段。

非常感谢任何帮助或建议。

我 95% 确定你的视图没有被调用。

您需要将您的应用程序作为 CMS apphook 进行集成,或者将您的 URL 硬编码到您的根目录中 urls.py 这是更容易的集成测试,因为它不依赖于 CMS 集成。

所以像这样将您的视图添加到您的 URL;

urlpatterns += i18n_patterns(
    url(r'^admin/', include(admin.site.urls)),  # NOQA
    url(r'^blog/$', views.index, name='blog'),
    url(r'^', include('cms.urls')),
)

然后在您的浏览器中转到 localhost:8000/blog/,您应该点击您的视图。

要确认您击中它,您可以对您的视图进行简单的修改;

def index(request):
    form = FilterForm()
    context = {
        'form': form,
    } 
    print "index hit"
    return render(request, 'blog.html', context)

如果您的视图被调用,您将在控制台中看到 "index hit"。