【问题标题】:I am trying to search the student result using registration# ? Help appricated我正在尝试使用registration#搜索学生结果?帮助表示赞赏
【发布时间】:2021-01-06 08:47:33
【问题描述】:

需要帮助来搜索数据,当我输入注册号时,它没有提供我在管理页面中输入的详细信息。需要帮忙 ??? home.html 我不知道如何放置数据以进行显示以及其他内容。


moodle.py

​​>
from django.db import models
from django.utils.encoding import smart_text

class ResultQuery(models.Model):
    name=models.CharField(max_length=150)
    dept_name=models.CharField(max_length=200)
    cgpa=models.CharField(max_length=50)
    reg_number=models.CharField(max_length=100)

    def __str__(self):
       return smart_text(self.name)

app==> url.py

​​>
from django.urls import path
from . import views
urlpatterns = [
    path('', views.home),
]

forms.py

​​>
from django import forms

class ResultForm(forms.Form):

      Reg_No  =forms.CharField(label="Registration Number")

views.py

​​>
from django.shortcuts import render

# Create your views here.
from django.shortcuts import render
from .forms import ResultForm
from .models import ResultQuery



def home(request):
    form=ResultForm(request.POST or None)
    template_name = "home.html"
    context = {"form": form}
    if form.is_valid():
        objects = ResultQuery.objects.filter(reg_number=form.cleaned_data['Reg_No'])
        context['objects'] = objects

    return render(request, template_name, context)

admin.py

​​>
from django.contrib import admin
from .models import ResultQuery

# Register your models here.
admin.site.register(ResultQuery),

home.html

<h1>Search Your Result</h1>

<form method="POST" action=" "> {% csrf_token %}
    {{ form }}
    <input type="submit" value="Submit"/>
</form>

注意:我想做搜索和显示数据,也帮助我在 HTML 页面中。 截图供参考。

【问题讨论】:

标签: python-3.x django django-models django-views django-forms


【解决方案1】:

您已设置context['objects'] = objects。这意味着您可以在模板中使用它来获取 ResultQuery 对象:

home.html

<h1>Search Your Result</h1>

<form method="POST" action=" "> {% csrf_token %}
    {{ form }}
    <input type="submit" value="Submit"/>
</form>

{% for result_query in objects %}
    {{ result_query.name }}
    {{ result_query.dept_name }}
    {{ result_query.cgpa }}
    {{ result_query. }}
{% endfor %}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-26
    • 2022-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-18
    • 1970-01-01
    相关资源
    最近更新 更多