【问题标题】:Can "list_display" show users input on django admin page?“list_display”可以在 django 管理页面上显示用户输入吗?
【发布时间】:2020-07-09 13:31:26
【问题描述】:

我需要在 django 的管理页面上查看用户的输入显示,根据我的阅读,我需要使用 list_display 但管理页面上没有任何内容。也许 views.py 中存在问题,但一切看起来都很好。 这是我的代码:

Views.py

    def process_e(request):

website_name = request.POST["website_name"]
product_name = request.POST["product_name"]
phone_number = request.POST["phone_number"]
website_details = request.POST["website_details"]
website_content = request.POST["website_content"]
created_objs = Enterprise.objects.create(website_name=website_name, product_name=product_name, phone_number=phone_number, website_details=website_details, website_content=website_content)
legth_of_todos = Enterprise.objects.all().count()
created_items = Enterprise.objects.all()

if request.method == 'POST':
    if request.POST.get('website_name') and request.POST.get('product_name') and request.POST.get('phone_number') and request.POST.get('website_details') and request.POST.get('website_content'):
        post=Enterprise()
        post.title= request.POST.get('website_name')
        post.content= request.POST.get('product_name')
        post.content= request.POST.get('phone_number')
        post.content= request.POST.get('website_details')
        post.content= request.POST.get('website_content')
        post.save()
        return render(request, 'enterprise.html', {"created_items":created_items})
    else:
        pass  
else:
        return render(request,'enterprise.html')

Admin.py

    from django.contrib import admin
    from service.models import Enterprise

    class PaymentsAdmin(admin.ModelAdmin):
        list_display = ('website_name', 'product_name', 'phone_number', 'website_details', 
    'website_content')

    admin.site.register(Enterprise, PaymentsAdmin)

模型.py

    from django.db import models

    class Enterprise(models.Model):
        website_name = models.CharField(max_length=50)
        product_name = models.CharField(max_length=200)
        phone_number = models.CharField(max_length=200)
        website_details = models.CharField(max_length=200)
        website_content = models.CharField(max_length=200)

HTML

   <div class="form-group">
        <label>Website name</label>
        <input type="text" name="website_name" class="form-control" placeholder="First name">
    </div>
    <div class="form-group">
        <label>Product name (if there is)</label>
        <input type="text" name="product_name" class="form-control" placeholder="First name">
    </div>
    <div class="form-group">
        <label>Phone number</label>
        <input type="text" claa="phone_name" class="form-control" placeholder="First name">
    </div>
    <div class="form-group">
        <label for="exampleFormControlTextarea1">What kind of content should your website upload?</label>
        <textarea class="form-control" name="website_content" rows="3"></textarea>
    </div>
    <div class="form-group">
        <label for="exampleFormControlTextarea1">Website details</label>
        <textarea class="form-control" name="website_details" rows="3"></textarea>
    </div>

【问题讨论】:

    标签: python html django sqlite


    【解决方案1】:

    是的。 list_display 用于在模型的 ListView 中显示模型的字段。您还需要至少一个模型实例。

    【讨论】:

    • 嘿,谢谢!模型实例是什么意思?
    • 在这种情况下,创建一个企业项目。
    • 我创建了一个项目,请您检查 views.py 文件,它仍然无法正常工作。在 html 中,我还在 for 循环中添加了 {{ created_item.variable_name }}。
    • list_display 仅适用于 /admin/ 面板,前提是您使用他们提供的默认 .html。
    猜你喜欢
    • 2016-10-03
    • 2013-01-27
    • 1970-01-01
    • 1970-01-01
    • 2017-06-21
    • 2018-02-12
    • 2011-05-09
    • 1970-01-01
    • 2011-01-21
    相关资源
    最近更新 更多