Django-CMS - Django Model Form无法渲染

时间:2018-05-03 07:32:42

标签: python django django-forms django-cms

我在Django中使用Django-CMS构建一个新站点,需要一个表单来过滤JSON结果并返回过滤后的集合。

我的问题是,最初,我甚至无法渲染Django模型表单但我可以使CSRF令牌工作,因此表单在技术上呈现,但输入/字段不是出现了。

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.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令牌正在显示,任何地方都没有任何错误,但它根本就没有显示字段。

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

1 个答案:

答案 0 :(得分:1)

我95%肯定你的观点没有被调用。

您需要将您的应用程序集成为CMS apphook或将您的网址硬编码到根目录urls.py中,这是一种更简单的集成测试,因为它不依赖于CMS集成。

因此,请将您的观点添加到您的网址中

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/&amp;你应该看看你的观点。

要确认您是否正在点击它,您可以对您的观点做一个简单的修改;

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

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