2015-03-08 154 views
0

我想製作一個可以上傳圖片的旋轉木馬。但是,當我點擊上傳按鈕時,它會切換到另一個頁面,而不是僅將上傳選擇面板作爲「有意」提供給我。誰能告訴我爲什麼這可能是?問題與點擊按鈕點擊

Views.py

from django.shortcuts import render 
from django.shortcuts import render, redirect 
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.core.urlresolvers import reverse 
from django.conf import settings 
from webportal.forms.forms import DocumentForm 
from webportal.models import Document 
is_server = True 
def list(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(reverse('webportal.views.list')) 
    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/carousel.html', 
     {'documents': documents, 'form': form,}, 
     context_instance=RequestContext(request) 
    ) 

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"> 
       <!-- Indicators --> 
       <ol class="carousel-indicators"> 
        <li data-target="#myCarousel" data-slide-to="0" class="active"></li> 
        <li data-target="#myCarousel" data-slide-to="1"></li> 
        <li data-target="#myCarousel" data-slide-to="2"></li> 
       </ol> 
       <div class="carousel-inner" role="listbox"> 
        {% for document in documents %} 
<div class="item {% if forloop.first %} active {% endif %}"> 
    <div class="row"> 
     <div class="col"> 
     <img src = "{{STATIC_URL}}img" > 
     </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:list' %}" 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> 

形式:

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

型號:

class Document(models.Model): 
    docfile = models.ImageField(upload_to='webportal/static/img/') 
+0

你可以發佈你的表單和模型嗎? – Selcuk 2015-03-08 22:31:47

+0

完成。添加模型和表單 – Jf2k 2015-03-08 22:40:54

+0

我不確定你要做什麼,但是這行代碼是這樣的:

'會在點擊提交(上傳)按鈕時將您重定向到「操作」值。你想上傳一張照片,並立即看到它在旋轉木馬上顯示嗎? – vishen 2015-03-08 23:52:01

回答

1

我不確定您的意思是「只將上傳選擇面板作爲有意使用」。

首先,如果您提交的表單無效,則返回空表單會出現問題,您應該返回現有表單並顯示錯誤。

您的表單在點擊上傳按鈕時會向您的服務器發送POST請求,您的視圖會生成一個全新的HTML頁面作爲響應。 HTTP是無狀態的,這意味着無論您的瀏覽器顯示如何,基本上都不再相關。所以自然你會看到一個新的頁面。