Django模板/查看轮播问题 [英] Django template/view issues with carousel

查看:201
本文介绍了Django模板/查看轮播问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OK,所以这里是这样的交易:

OK, so here's the deal:

这是目前我在做的工作:

This is currently what I'm working on:

看到两个箭头顶端?那是图片的转盘应该是哪里。但是,这个转盘中没有图片。也就是说,直到我点击上传按钮。

See the two arrows at the top? That's where a carousel of pictures should be. However, there are no pictures in this carousel. That is, until I click the 'Upload' Button.

所以,我的目标是让图片出现在第一页上,再点击上传按钮

So, my goal is to make the pictures appear on the first page before I even click the 'upload' button.

如何解决这个问题?

我的代码:

{% extends 'webportal/defaults.html' %}
{% block body %}
    {% include 'webportal/carousel.html' %}

    <br/>
    <div class="container">
        <div class="row">
            <div class="col-md-12">
                <p> So far we have been able to establish what the user tables will be have created or are in process of
                    creating
                    the ability to create, update, destroy users.</p>
            </div>
        </div>
        <div class="row">
            <div class="col-md-12">
                <p> For now this ability is limited to the main user table, however the models will be easily extended
                    to other
                    tables as they are needed</p>
            </div>
        </div>
        <div class="row">
            <div class="col-md-12">
                <p> Also not a lot of thought has gone into styling yet, we know that the page is suppose to resemble
                    the parent
                    organizations page. Right now we have been more focused on getting each individual component
                    working. Later we
                    will merge them into a whole. </p>
            </div>
        </div>
    </div>


    {% include 'webportal/info_row.html' with one=one two=two three=three %}
{% endblock %}

Carousel.html:

Carousel.html:

{% load staticfiles %}
{% load filename %}

<div class="container">
    <div class="row">
        <div class="col-md-12">
            <div id="myCarousel" class="carousel slide" data-ride="carousel">
                <div class="carousel-inner" role="listbox">
                    {% for document in documents %}
                        <div class="item {% if forloop.first %} active {% endif %}">
                           <div class="row">
                             <div class="col">
                               <li><a href = "{{document.docfile.url}}">{{document.docfile.name}}</a></li>
                               <img src = "{{STATIC_URL}}img/{{document|filename}}" >
                               <p align="center"><form style="text-align:center" action="{% url 'webportal:delete' %}" method="post" enctype="multipart/form-data">
                                   {% csrf_token %}
                                   <p>{{ form.non_field_errors }}</p>
                                   <p>{{ form.Document.label_tag }} {{ form.Document.help_text }}</p>
                                   <p>
                                       {{ form.Document.errors }}
                                       {{ form.Document.docfile }}
                                   </p>
                                   <p><input type="submit" value="Delete" /></p>
                               </form></p>
                             </div>
                           </div>
                         </div>
                    {% endfor %}
                </div>
                <a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev">
                    <span class="glyphicon glyphicon-chevron-left"></span>
                    <span class="sr-only">Previous</span>
                </a>
                <a class="right carousel-control" href="#myCarousel" role="button" data-slide="next">
                    <span class="glyphicon glyphicon-chevron-right"></span>
                    <span class="sr-only">Next</span>
                </a>
            </div>
            <!-- /.carousel -->
        </div>
    </div>
    <form action="{% url 'webportal:carousel' %}" method="post" enctype="multipart/form-data">
        {% csrf_token %}
        <p>{{ form.non_field_errors }}</p>
        <p>{{ form.docfile.label_tag }} {{ form.docfile.help_text }}</p>
        <p>
            {{ form.docfile.errors }}
            {{ form.docfile }}
        </p>
        <p><input type="submit" value="Upload" /></p>
    </form>
</div>



Views.py:



Views.py:

from django.shortcuts import render
from django.shortcuts import render, redirect, get_object_or_404
from django.contrib.auth import authenticate, login
from webportal.views.authentication import LoginForm
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.http import HttpResponseRedirect
from django.http import HttpResponse
from django.core.urlresolvers import reverse
from django.conf import settings
from webportal.forms.forms import DocumentForm
from webportal.models import Document, DeleteForm
is_server = True
def delete(request, my_id):
    Deleted=get_object_or_404(Document, docfile=my_id)
    if request.method=='POST':
        form=DeleteForm(request.POST, instance=Deleted)
        if form.is_valid():
            Deleted.delete()
            return HttpResponseRedirect('http://127.0.0.1:8000/alzheimers/')
    else:
        form=DeleteForm(instance=Deleted)
    return render_to_response(
        'webportal/index.html',
        {'documents': documents, 'form': form,},
        context_instance=RequestContext(request)
    )

# Redirect to the document list after POST
def carousel(request):
    # Handle file upload
    if request.method == 'POST':
        form = DocumentForm(request.POST, request.FILES)
        if form.is_valid():
            newdoc = Document(docfile = request.FILES['docfile'])
            newdoc.save()

            # Redirect to the document list after POST
            return HttpResponseRedirect('http://127.0.0.1:8000/alzheimers/')
   else:
       form = DocumentForm() # A empty, unbound form
# Load documents for the list page
   documents = Document.objects.all()
#documents=DocumentForm().
# Render list page with the documents and the form
   return render_to_response(
    'webportal/index.html',
    {'documents': documents, 'form': form,},
    context_instance=RequestContext(request)
)



Models.py:



Models.py:

class Document(models.Model):
    docfile = models.ImageField(upload_to='webportal/static/img/')
class DeleteForm(ModelForm):
    class Meta:
        model=Document
        fields=[]



Forms.py:



Forms.py:

class DocumentForm(forms.Form):
    docfile = forms.ImageField(label='Select a file', help_text='max. 42 megabytes')

如果我想要做的是不可能的,我不会感到惊讶如果有办法这样做,它是否涉及到Ajax?

I wouldn't be surprised if what I'm trying to do is impossible. If there is a way to do this, does it involve Ajax?

推荐答案

.py文件是Python代码。 在Python中,领先的空白问题

The .py files are Python code. In Python, leading whitespace matters.

Views.py 中, carousel 函数不包含有效的Python代码:

In Views.py, the carousel function does not contain valid Python code:

def carousel(request):
    # Handle file upload
    if request.method == 'POST':
        form = DocumentForm(request.POST, request.FILES)
        if form.is_valid():
            newdoc = Document(docfile = request.FILES['docfile'])
            newdoc.save()

            # Redirect to the document list after POST
            return HttpResponseRedirect('http://127.0.0.1:8000/alzheimers/')
else:

 form = DocumentForm() # A empty, unbound form
# Load documents for the list page
documents = Document.objects.all()
#documents=DocumentForm().
# Render list page with the documents and the form
return render_to_response(
    'webportal/index.html',
    {'documents': documents, 'form': form,},
    context_instance=RequestContext(request)
)

可能是:

def carousel(request):
    # Handle file upload
    if request.method == 'POST':
        form = DocumentForm(request.POST, request.FILES)
        if form.is_valid():
            newdoc = Document(docfile = request.FILES['docfile'])
            newdoc.save()

            # Redirect to the document list after POST
            return HttpResponseRedirect('http://127.0.0.1:8000/alzheimers/')

    else:

        form = DocumentForm() # A empty, unbound form
        # Load documents for the list page
        documents = Document.objects.all()
        #documents=DocumentForm().
        # Render list page with the documents and the form
        return render_to_response(
            'webportal/index.html',
            {'documents': documents, 'form': form,},
            context_instance=RequestContext(request)
        )

但它不是清除 else 部分是否属于第一个或第二个,如果构造。

But it is not clear if the else part belongs to the first or the second if construct.

这篇关于Django模板/查看轮播问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆