【问题标题】:How to retrieve the data from the database and display it in a textbox for users to update it如何从数据库中检索数据并将其显示在文本框中以供用户更新
【发布时间】:2021-12-19 05:07:02
【问题描述】:

我有一个网页,它应该允许用户更新数据库中的数据,但数据无法在文本框中显示,我该如何让它能够在文本框中显示?

这是我当前的网页,用户需要在其中手动输入他们想要在文本框中更新的所有数据。

我想要的是这样,数据已经从数据库中检索出来供用户查看,假设他们要更新客户名称,他们只需要更改客户名称并单击提交按钮即可更新它,如何使它能够检索数据?

views.py

@login_required()
def updatedata(request, id):
    photo = Photo.objects.get(id=id)
    if request.method == 'POST':
        Photo.mcoNum = request.POST.get('mcoNum')
        Photo.reception = request.POST.get('reception')

        form = UpdateForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('logdata')
    else:
        form = UpdateForm

    return render(request, 'updatedata.html', {'form': form})

updatedata.html

<!doctype html>
{% extends "home.html" %}
{% block content %}
{% load static %}

<br><br>
    <h2 class="text-center">Edit Log Data</h2>
    <hr>
    <div class="col-md-6 offset-md-3">
        <form method="POST">
            {% csrf_token %}

            {{ form.as_p }}
            <input type="submit" value="Update" class="btn btn-secondary">
        </form>
    </div>
{% endblock %}

forms.py

class UpdateForm(forms.ModelForm):
    mcoNum = forms.CharField(label="", widget=forms.TextInput(
        attrs={"class": "form-control", "placeholder": "MCO Number"}))
    reception = forms.CharField(label="", max_length=100, widget=forms.TextInput(
        attrs={"class": "form-control", "placeholder": "reception"}))
    partno = forms.CharField(label="", max_length=100, widget=forms.TextInput(
        attrs={"class": "form-control", "placeholder": "Part Number"}))
    serialno = forms.CharField(label="", max_length=100,
                               widget=forms.TextInput(attrs={"class": "form-control", "placeholder": "Serial Number"}))
    Customername = forms.CharField(label="", max_length=100,
                               widget=forms.TextInput(attrs={"class": "form-control", "placeholder": "Customer Name"}))
    class Meta:
        model = Photo
        fields = ("mcoNum", "reception", "partno", "serialno", "Customername",)

【问题讨论】:

    标签: html django django-views django-forms


    【解决方案1】:

    试试这个。你必须在表单中传递实例(照片)。

    @login_required()
    def updatedata(request, id):
        photo = Photo.objects.get(id=id)
        if request.method == 'POST':
            Photo.mcoNum = request.POST.get('mcoNum')
            Photo.reception = request.POST.get('reception')
    
            form = UpdateForm(request.POST,instance=photo)
            if form.is_valid():
                form.save()
                return redirect('logdata')
        else:
            form = UpdateForm(instance=photo)
    
        return render(request, 'updatedata.html', {'form': form})
    

    【讨论】:

    • 诶,不行,先试试,文本框就是不显示数据
    • @Shadowwalker 创建一个聊天,我们将讨论它。
    • @Shadowwalker 以&lt;form action = '{% url 'your-url-of-updatedata' %} method="POST"&gt;的形式在动作中添加url
    【解决方案2】:

    您必须将模型数据从视图传递到模板 您可能必须在您的视图中起作用,这里引用一页是示例

    # here is first function that send information to your fields
    # i suggest you to use id in url and here to recognize which record you are updating
    def editbox(request,id)
        # here we get informations from model database
        data = yourmodel.objects.get(id=id) #here yourmodel is your actual model name that you have to import above
        return render(request,'updatedata.html',{"data":data}) # and here we pass to html
    
    
    # secondly you have to update and edit your data
    def FunctionName(request,id):
        updateme = yourmodel.objects.get(id=id) # here we have to use again to recognize which data we're updating
        form = yourupdateform(request.POST,instance=updateme) # it send data 
        if form.is_valid: 
            form.save()
            return redirect('logdata') # redirect if you want 
    

    现在您需要制作表单:

    from django import forms
    from .models import yourmodel
    
    class formname(forms.ModelForm):
        model=yourmodel
        fields="__all__" # this will update all the fields of model class
    

    您必须编辑 url 以将 id 从 url 传递给这些函数才能工作

       # you need to have two function, one of them is for editing and ecyrything is there and other is just for update
       # replace your function name there
       path('edit/<int:id>',views.youreditfunction')
       path('update/<int:id>',views.yourupdatefunction')
    

    现在是时候制作html了:

       <br><br>
        <h2 class="text-center">Edit Log Data</h2>
        <hr>
        <div class="col-md-6 offset-md-3">
            <form action="/update/{{ data.id }}" method="post">
                <!-- here you can style your forms and etc i dont focus on here -->
                {% crf_token %}
    
                # here is an example of what you can do you can write as much as you want
                <input type="text" class="form-control" value="{{ data.username }}">
    
                <input type="submit" value="Update">
            </form>
        </div>
    

    我认为这种方式有效,但不是 100%

    【讨论】:

      猜你喜欢
      • 2013-03-23
      • 1970-01-01
      • 1970-01-01
      • 2018-02-26
      • 1970-01-01
      • 1970-01-01
      • 2012-12-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多