简体   繁体   English

如何让 Django 在浏览器中呈现?

[英]How can I make Django render in the browser?

I'm trying to utilize Django forms.ModelForm function.我正在尝试利用 Django forms.ModelForm 函数。 However, I can not get it to render in the browser (Firefox and Chrome tested).但是,我无法让它在浏览器中呈现(经过 Firefox 和 Chrome 测试)。 In both browser inspection of code, the table\\form does not show and there is no error coming from Django at all.在两种浏览器的代码检查中,表\\表单都没有显示,并且根本没有来自 Django 的错误。 The only thing that shows from the html file is the "Save button" Is there something I am missing here? html 文件中唯一显示的是“保存按钮” 我在这里遗漏了什么吗?

In Models.py在 Models.py 中

from django.db import models


class Product_sell_create(models.Model):
    product_product_sell = models.CharField(max_length=120)
    product_price_sell = models.DecimalField(decimal_places=2, max_digits=500)
    product_volume_sell = models.DecimalField(decimal_places=2, max_digits=500)
    product_location_sell = models.CharField(max_length=120)
    product_description_sell = models.TextField(blank=False, null=False)

Forms.py表单.py

from django import forms
from .models import Product_sell_create

class ProductName(forms.ModelForm):
    class Meta:
        model = Product_sell_create
        fields = [
            'product_product_sell',
            'product_volume_sell',
            'product_price_sell',
            'product_location_sell',
            'product_description_sell'
        ]

Views.py视图.py

from django.shortcuts import render
from .forms import ProductName


def products_create_view(request):
    form = ProductName(request.POST or None)
    if form.is_valid():
        form.save()
        form = ProductName()

    context = {
        'form': form
    }

    return render(request, "sell.html", context)

sell.html出售.html

{% include 'navbar.html' %}
<h1> Upper test</h1>

<form>
    {% csrf_token %}
    {{ form.as_p }}
    <input type="submit" value="Save" />
</form>

<h1> TEST </h1>

{% block content %}
{% endblock %}

just did it, you would have problems POSTing your object too:刚刚做到了,你也会在发布你的对象时遇到问题:

views.py:视图.py:

from django.shortcuts import render, redirect
from .forms import ProductName
from .models import Product_sell_create

def products_create_view(request):
    if request.method == 'POST':
        form = ProductName(request.POST)
        if form.is_valid():
            prod = form.save(commit=False)
            prod.save()
            return redirect('../thanks')
    else:
        form = ProductName()

        context = {
            'form': form
        }

        return render(request, "form_test.html", context)


def thanks_view(request):
    query = Product_sell_create.objects.all()
    return render (request, 'thanks.html', {'query' : query})

forms.py and models.py keeps the same forms.py 和 models.py 保持不变

sell.html:出售.html:

<h1> Upper test</h1>

<form method="POST">
    {% csrf_token %}
    {{ form.as_p }}
    <input type="submit" value="Save" />
</form>

<h1> TEST2 </h1>

thanks.html:谢谢.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>{{ query }}</h1>
    <h2>THANKS</h2>
</body>
</html>

在此处输入图片说明

did you create the 'sell.html' inside a 'templates' folder in your app folder?您是否在应用程序文件夹的“模板”文件夹中创建了“sell.html”?

MyApp/templates/sell.html我的应用程序/模板/sell.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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