简体   繁体   中英

How can I access form(in template) which is defined in another app(views) in Django

New to Django. So please help me. What my goal here is that I want to access form from another application(cart) to my current application(shoppApp).

views.py(cart application):

from django.shortcuts import render, redirect, get_object_or_404
from django.views.decorators.http import require_POST
from shoppApp.models import Product
from .cart import Cart
from .forms import CartAddProductForm



@require_POST
def cart_add(request, product_id):
    cart = Cart(request)
    product = get_object_or_404(Product, id=product_id)
    form = CartAddProductForm(request.POST)
    if form.is_valid():
        cd = form.cleaned_data
        cart.add(product=product,quantity=cd['quantity'],update_quantity=cd['update'])
    return redirect('cart:cart_detail')
def cart_remove(request, product_id):
    cart = Cart(request)
    product = get_object_or_404(Product, id=product_id)
    cart.remove(product)
    return redirect('cart:cart_detail')
def cart_detail(request):
    cart = Cart(request)
    for item in cart:
        item['update_quantity_form'] = CartAddProductForm(initial={'quantity': item['quantity'],'update': True})
    return render(request, 'cart/detail.html', {'cart': cart})
def product_detail(request, id, slug):
    product = get_object_or_404(Product, id=id,slug=slug,available=True)
    cart_product_form = CartAddProductForm()
    return render(request,'shop/product/detail.html',{'product': product,'cart_product_form': cart_product_form})

forms.py(cart application)

from django import forms
PRODUCT_QUANTITY_CHOICES = [(i, str(i)) for i in range(1, 21)]
class CartAddProductForm(forms.Form):
    quantity = forms.TypedChoiceField(choices=PRODUCT_QUANTITY_CHOICES,coerce=int)
    update = forms.BooleanField(required=False,initial=False,widget=forms.HiddenInput)

details.html(shoppApp application)

{% extends "shop/base.html" %}
{% load static %}
    {% block title %}
        {{ product.name }}
    {% endblock %}
    {% block content %}
        <div class="product-detail">
            <img src="{% if product.image %}{{ product.image.url }}{% else %}{% static 'img/no_img.jpg' %}{% endif %}">
            <h1>{{ product.name }}</h1>
            <h2><a href="{{ product.category.get_absolute_url }}">{{product.category }}</a></h2>
            <p class="price">${{ product.price }}</p>
            <form action="{% url 'cart:cart_add' product.id %}" method="post">
                {% csrf_token %}
                {{ cart_product_form }}
                <input type="submit" value="Add to cart">
            </form>
            {{ product.description|linebreaks }}
        </div>
       
       
    {% endblock %}

views.py(shoppApp application)

from django.shortcuts import render

# Create your views here.
from django.shortcuts import render, get_object_or_404
from .models import Category, Product
from cart.cart import Cart
from cart.forms import CartAddProductForm


def product_list(request, category_slug=None):
    category = None
    categories = Category.objects.all()
    products = Product.objects.filter(available=True)
    if category_slug:
        category = get_object_or_404(Category, slug=category_slug)
        products = products.filter(category=category)
    return render(request,'shop/product/list.html',{'category': category,'categories': categories,'products': products})

def product_detail(request, id, slug):
    product = get_object_or_404(Product,id=id,slug=slug,available=True)
    return render(request,'shop/product/detail.html',{'product': product})

project structure:
项目结构 项目结构
Please correct me if the way I am trying is wrong. Thankyou in advance.

Simply you can import form from cart to shopping app by doing this:

from cart.forms import formname

EX: from cart.forms import UserForm

Add this above code inside shopping app.

This will solve your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM