1

i am trying to populate existing data in the database into a dropdownlist where i want just to make the data appear inside the dropownlist.

when i run the system it display the below error :

DoesNotExist at / testmodel3 matching query does not exist. Request Method: POST Request URL: http://127.0.0.1:8000/ Django Version: 2.1.7 Exception Type: DoesNotExist Exception Value: testmodel3 matching query does not exist.

it says that the error is at line 14

tmn3 = testmodel3.objects.get(name = tmn) 
  1. i create class in models.py
  2. map the page in the url.py
  3. create form
  4. create a template
  5. create a function in views.py.

models.py

class testmodel3(models.Model):
    name = models.CharField(max_length=100)

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

url.py

from django.contrib import admin
from django.urls import path
from.views import *

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', home),

]

forms.py

from django import forms
from .models import *

class drodownForm(forms.ModelForm):

    class Meta:
        model = testmodel
        fields = ('name',)

home.html

    <!DOCTYPE html>
    <html>
    <head>
        <title></title>
    </head>
    <body>

    <form method="POST">{% csrf_token %}
        <table>
            <tr><td>mouhafazat name:</td>
                <td>
                    <select name = "dropdwn1">
                        {% for testmodel in testmodel3name %}
                            <option value="{{ testmodel3.name }}">{{ testmodel3.name }}</option>
                        {% endfor %}
                    </select>
                </td>
                <td><input type="submit" value="click" /></td>
            </tr>
        </table>


</form>

</body>
</html>

views.py

from django.shortcuts import render
from django.http import HttpResponse
from testapp.models import *
from testapp.forms import drodownForm


def home(request):
    testmodel3name = testmodel3.objects.all()
    print(testmodel3name)
    tmn = request.POST.get('dropdwn1')
    if request.method =='GET':
        form = drodownForm()
    else:
        tmn3 = testmodel3.objects.get(name = tmn)
        print("test for dropdown")

    return render(request,'./home.html',{'form': form, 'testmodel3name': testmodel3name})

i expect to view the stored data in a dropdown list

Django Dg
  • 97
  • 4
  • 19
  • Possible duplicate of [matching query does not exist Error in Django](https://stackoverflow.com/questions/5508888/matching-query-does-not-exist-error-in-django) – Davit Tovmasyan Feb 13 '19 at 09:00

0 Answers0