将图像上传到轮播的问题[重复]

时间:2022-04-07 07:47:03

This question already has an answer here:

这个问题在这里已有答案:

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.

因此,我的目标是在我点击“上传”按钮之前让图片显示在第一页上。

How can I fix this problem? I'm kind of a noob at Django, and writing this code is like pulling teeth.

我该如何解决这个问题?我是Django的一个菜鸟,编写这段代码就像拔牙一样。

My code:

我的代码:

Index.html

{% 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:

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:

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

Forms.py:

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

URls.py:

URls.py:

from django.conf.urls import patterns, url 

from webportal.views import views, register, authentication, welcome, search, profile, event, csv_export, role_creation, \ 
    edit_event, reports, accounts 



urlpatterns = patterns('', 
                       url(r'^reports', reports.report_page), 
                       url(r'^search/criteria', search.get_criteria), 
                       url(r'^search', search.search_page), 
                       url(r'^register', register.SignUpView.as_view(), name="register"), 
                       url(r'^login', authentication.login_view, name="login"), 
                       url(r'^carousel', views.carousel, name="carousel"), 
                       url(r'^delete', views.delete, name="delete"),  
                       url(r'^profile', profile.ProfileView.as_view(), name="profile"), 
                       url(r'^welcome', welcome.WelcomeView.as_view(), name="welcome"), 

                       url(r'^event/creation', event.Creation.as_view(), name="event_creation"), 
                       # url(r'^role_creation', role_creation.Creation.as_view(), name="role_creation"), 
                       url(r'^csv_export', csv_export.CSVExport.as_view(), name="csv_export"), 
                       url(r'^csv_import', reports.upload_file, name="csv_import"), 
                       url(r'^logout$', 'django.contrib.auth.views.logout', {'next_page': '/alzheimers/'}), 
                       url(r'^edit_event', edit_event.EditView.as_view(), name='edit_event'), 
                       url(r'^parse_ajax', profile.parse_ajax), 
                       url(r'^event/role_ajax', role_creation.ajax), 
                       url(r'^event/all', event.view_all), 
                       url(r'^event/information', event.ajax), 
                       url(r'^accounts/personal', accounts.personal), 
                       url(r'^accounts/create', accounts.create), 
                       url(r'^accounts/edit', accounts.edit), 
                       url(r'^accounts/remove', accounts.remove), 
                       url(r'^$', views.bootstrap), 
)

If there is a way to do this, does it involve ajax? I asked this again because of a lack of an adequate answer.

1 个解决方案

#1


1  

You should pass the documents context variable to the template in the view which handles the /alzheimers/ page.

您应该将文档上下文变量传递给处理/ alzheimers / page的视图中的模板。

UPDATE: The view you should change is the views.boostrap(). So rendering of the template will be something like this:

更新:您应该更改的视图是views.boostrap()。因此,模板的渲染将是这样的:

def bootstrap(request):
   ...
   return render_to_response('webportal/bootstrap.html',
                             {'documents': Document.objects.all()},
                             context_instance=RequestContext(request)
   )

#1


1  

You should pass the documents context variable to the template in the view which handles the /alzheimers/ page.

您应该将文档上下文变量传递给处理/ alzheimers / page的视图中的模板。

UPDATE: The view you should change is the views.boostrap(). So rendering of the template will be something like this:

更新:您应该更改的视图是views.boostrap()。因此,模板的渲染将是这样的:

def bootstrap(request):
   ...
   return render_to_response('webportal/bootstrap.html',
                             {'documents': Document.objects.all()},
                             context_instance=RequestContext(request)
   )