django问题,表格不显示。

时间:2015-08-13 06:44:08

标签: django django-models django-forms django-views

我在Django中遇到问题,我正在尝试制作一个适用于管理员方面的表单,但我想在前端看到相同的表单,这样做了一些错误,所以它不显示。

model.py类

class Product(models.Model):
    title = models.CharField(max_length=120)
    description = models.TextField(null=True, blank=True)
    category = models.ManyToManyField(Category, null=True, blank=True)
    price = models.DecimalField(decimal_places=2, max_digits=100, default=29.99)
    sale_price = models.DecimalField(decimal_places=2, max_digits=100,\
                                            null=True, blank=True)
    slug = models.SlugField(unique=True)
    timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
    updated = models.DateTimeField(auto_now_add=False, auto_now=True)
    active = models.BooleanField(default=True)
    update_defaults = models.BooleanField(default=False)

    def __unicode__(self):
        return self.title

    class Meta:
        unique_together = ('title', 'slug')

    def get_price(self):
        return self.price

    def get_absolute_url(self):
        return reverse("single_product", kwargs={"slug": self.slug})

我的admin.py

from .models import Product, ProductImage, Variation, Category
admin.site.register(ProductImage)
admin.site.register(Variation)
admin.site.register(Category)        

from products.models import Product
admin.site.register(Product)

html文件

{% extends 'base.html' %}
{% load staticfiles %}    
{% block head_title %}
Products || 
{% endblock %}

{% block styles %}
.jumbotron {
    /*color: red;*/
}
{% endblock %}


{# Comments here #}

{% block content %}    
<div class='col-sm-6 col-sm-offset-3' style='margin-top: 50px;'>
<h1>{{ form_title }}</h1>    
   <form  method="post" enctype="multipart/form-data">
    <div  style="background-color:#f2f2f2" ><br />
    <div style="width:25%; margin:0px auto;">
       {% csrf_token %}

       {{ form.as_table}}

         <input type='submit' value='{% if submit_btn %}{{ submit_btn }}{% else %}Submit{% endif %}' class='btn btn-default' />
        </div>
        </div>
    </form>
</div>    
{% endblock %}

view.py

      from django.shortcuts import render, Http404


from marketing.forms import EmailForm
from marketing.models import MarketingMessage, Slider


from .models import Product, ProductImage


def search(request):
    try:
        q = request.GET.get('q')
    except:
        q = None

    if q:
        products = Product.objects.filter(title__icontains=q)
        context = {'query': q, 'products': products}
        template = 'products/results.html'  
    else:
        template = 'products/home.html' 
        context = {}
    return render(request, template, context)


def home(request):
    sliders = Slider.objects.all_featured()
    products = Product.objects.all()
    template = 'products/home.html' 
    context = {
        "products": products,
        "sliders": sliders,
        }
    return render(request, template, context)


def all(request):
    products = Product.objects.all()
    context = {'products': products}
    template = 'products/all.html'  
    return render(request, template, context)

def deals(request):

    products = Product.objects.all()
    template = 'deals.html' 
    context = {
           "products": products,
          }
    return render(request, template, context)

def single(request, slug):
    try:
        product = Product.objects.get(slug=slug)
        #images = product.productimage_set.all()
        images = ProductImage.objects.filter(product=product)
        context = {'product': product, "images": images}
        template = 'products/single.html'   
        return render(request, template, context)
        except:
        raise Http404

1 个答案:

答案 0 :(得分:0)

你应该这样做:

def my_view(request):
    form = my_form()
    if request.method == "POST":
        form = my_form(request.POST, request.FILES)
        if form.is_valid():
            #do something with cleaned_data
            form.save()
        else:
            return render_to_response("your_template.html", {'form':form}, context_instance=RequestContext(request))
     else:
         form = my_form()
         return render_to_response("your_template.html", {'form':form}, context_instance=RequestContext(request))

并在您的模板中使用如下:

<form action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
{{form.as_p}}
</form>