通过按钮点击

时间:2018-02-04 05:34:16

标签: django

我是Django的新手,我建立了一个在线餐馆网站作为一项任务。我在将订单输入数据库时​​遇到问题。这是代码

models.py

from django.db import models
from django.contrib import admin
from django.contrib.auth.models import User
from django.db.models.signals import post_save

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    phone = models.IntegerField()
    address = models.CharField(max_length=500)

def create_profile(sender , **kwargs):
    if kwargs['created'] :
        user_profile = UserProfile.objects.create(user=kwargs['instance'])

post_save.connect(create_profile , sender=User)



class Category(models.Model):
    category_title = models.CharField(max_length=100 , unique=True)

    def __str__(self):
        return self.category_title


class Order (models.Model):
    time = models.DateTimeField(auto_now_add=True)
    total = models.IntegerField()
    created_by = models.ForeignKey(User, related_name='orders')


class Item (models.Model):
    name = models.CharField(max_length=100 ,unique=True)
    details = models.CharField(max_length=1000)
    price = models.IntegerField()
    item_logo = models.FileField()
    category = models.ForeignKey(Category, related_name="items" ,on_delete= models.CASCADE)
    order = models.ForeignKey(Order, related_name="orderItems", null=True, blank=True)

    def __str__(self):
        return self.name


class ItemInline(admin.TabularInline):
    model = Item

class CategoryAdmin(admin.ModelAdmin):
    inlines = [
        ItemInline,
    ]

views.py

from django.http import Http404
from  .models import Category, Item , Order
from django.shortcuts import render, redirect
from django.core.urlresolvers import reverse_lazy
from django.contrib.auth import authenticate , login
from django.views import generic
from django.views.generic import View
from .forms import UsrForm


class IndexView(generic.ListView) :
    template_name = 'res/base.html'
    context_object_name = 'all_categories'

    def get_queryset(self):
        return Category.objects.all()


def detail(request, category_name):
    try:
        category = Category.objects.get(category_title = category_name)
        all_categories = Category.objects.all()
        items = category.items.all()
    except Category.DoesNotExist:
        raise Http404("Category does not exist")

    return render(request, 'res/menu.html', {'items': items} , {'all_categories': all_categories})

这里是base.html(我希望当用户点击完成按钮时,使用div中选择的项目创建订单(id = selecteditems))

<!DOCTYPE html>
{% load staticfiles %}

<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Restaurant</title>

  <script src="js/site.js"></script>
  <link rel="stylesheet" href="{% static 'res/bootstrap/css/bootstrap.min.css' %}">
  <link rel="stylesheet" href="{% static 'res/main.css' %}">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="{% static 'res/bootstrap/js/bootstrap.min.js' %}"></script>
  <script src="{% static 'res/site.js' %}"></script>
</head>
<body>
    <header>
        <img src="{% static 'res/images/2.JPG' %}" alt="Happy Meal" width= "100% ;" height=150px ; >
    </header>

    <ul class="topnav">
        {% for category in all_categories %}
        <li><a href="{% url 'detail' category.category_title %}" onclick="addclass(this) ;return false;">{{ category.category_title }}</a></li>
        {% endfor %}
        <li><a href="#" onclick="addclass(this) ;return false;">About Us</a></li>

    </ul>
    <section>
    <div class="container-fluid">
      <h1 style="margin: 20px 10px 10px 10px ;">Choose Items</h1>
      <div class="row" >
        <div class="col-md-8" style="background-color: #FAFAFA ;" id="menu">
          <div id="pizza_menu">
              {% block content %}
              {% endblock %}
          </div>
        </div>
        <div class="col-md" style="background-color: #D9FFFA; font-size:18px; text-align: left ;" id="order">
          <h1 style="margin-bottom: 10px ; text-align: center ;">Your Order</h1>
          <div class="row">
              <div class="col-md-7" style="margin-top: 10px ;" id="selecteditems">

              </div>
              <div class="col-md-2" style="margin-top: 10px ;" id="pr">

              </div>
              <div class="col-md-3" style="margin-top: 8px ; text-align:right;" id="removebtn">

              </div>
              <div style="text-align: center;">
                  <button class="btn btn-primary">Finish</button>
              </div>
          </div>
        </div>
      </div>
    </div>
    </section>

</body>
</html>

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:1)

您需要查看Django forms。如果您希望自己的工作更轻松,可以考虑使用Django ModelForm。如果您想避免编写HTML并拥有干净漂亮的布局,可以使用Crispy forms

提示: base.html应该只包含大多数Django模板要继承的模板。任何特定页面都应该使用其他名称。