0

I'm having this value error when I run my menu function. Please help me solve this error. Thank you.

python code :

def index(request):
if not request.user.is_authenticated:
    return render(request, "register.html", {"message": None})
context = {
        "user": request.user,
        "regular": Regular.objects.get('name','price'),
        "sicilian": Sicilian.objects.get('name','price'),
        "subs": Subs.objects.get('name','price'),
        "salad": Salads.objects.get('name','price'),
        "topping": Toppings.objects.get('name'),
        "pasta": Pasta.objects.get('name','price'),
        "dinner": Dinner.objects.get('name','price'),
        }
return render(request, "user.html", context)

def login_view(request):
    username = request.POST["username"]
    password = request.POST["password"]
    user = authenticate(request, username=username, password=password)
    if user is not None:
        login(request, user)
        return HttpResponseRedirect(reverse("index"))
    else :
        return render(request,"login.html",{"message":"invalid  credentials"})

def logout_view(request):
    logout(request)
    return render(request,"login.html",{"message":"logged out"})

def register(request):
    username = request.POST["username"]
    password = request.POST["password"]
    email = request.POST.get("email", False)
    user = User.objects.create_user(username=username,email=email,password=password)
    return render(request,"login.html",{"message": "account created"})

def menu(request):
    regplac = str(request.POST["regplace"])
    sicplac = str(request.POST["sicplace"])
    subplac = str(request.POST["subplace"])
    salplac = str(request.POST["salplace"])
    toplac = str(request.POST["toplace"])
    paplac = str(request.POST["paplace"])
    dinplac = str(request.POST["dinplace"])
    return render(request,"place.html", reg=regplac, sic=sicplac, sub=subplac, sal=salplac, top=toplac, pap=paplac, din=dinplac)

html code :

<html>
<head>
    <title> logged in </title>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"></script>
</head>
<body>
    <h1 style="font-family:verdana; font-style:italic;">Welcome to our site !!!!</h1>
    <ul>
        <li>currently logged in as {{ user.username }}</li>
        <li><a href="{% url 'logout' %}">logout</a></i>
    </ul>
    <hr>
    <form action="{% url 'menu' %}" method="post">
        {% csrf_token %}
        <select name="regplace">
            {% for r in regular %}
            <option>{{r}}</option>
            {% endfor %}
        </select>
        <select name="sicplace">
            {% for s in sicilian %}
            <option>{{s}}</option>
            {% endfor %}
        </select>
        <select name="subplace">
            {% for u in subs %}
            <option>{{u}}</option>
            {% endfor %}}
        </select>
        <select name="salplace">
            {% for a in salad %}
            <option>{{a}}</option>
            {% endfor %}
        </select>
        <select name="toplace">
            {% for t in topping %}
            <option>{{t}}</option>
            {% endfor %}
        </select>
        <select name="paplace">
            {% for p in pasta %}
            <option>{{p}}</option>
            {% endfor %}
        </select>
        <select name="dinplace">
            {% for d in dinner %}
            <option>{{d}}</option>
            {% endfor %}
        </select>
        <input type="submit" value="place order"/>
    </form>
</body>

9
  • Are you using the same names in the html for loops that were sent in the request? Recommend: View page source from context menu in browser and see what html is generated. – DinoCoderSaurus Apr 30 '20 at 13:13
  • @DinoCoderSaurus Nope I'm not using the same names, the names sent in request.post are of the select tag. And I checked my page source, there are no option tags in my source. I don't know where the problem is. – stormcloak Apr 30 '20 at 13:22
  • If the html code displayed is place.html then there is no sicilian or subs or salad etc. If that's not the case, then I do not understand the relationship between the python code and the html code displayed. Perhaps validate the html that is generated as with Nu HTML checker – DinoCoderSaurus Apr 30 '20 at 13:32
  • @DinoCoderSaurus I've posted the whole python code. Actually I had to put that variables from menu function to the index function. But now its showing a value error. When I used "Regular.objects.all()" in index, it showed the options, but the options were displayed like -> (obejct 1, object 2 ) in the dropdown box. – stormcloak May 2 '20 at 5:53
  • Progress anyway, since program is finally giving a drop down list. Now to the get regular et al into the format you want; or change the iteration (eg {% for r in regular %}) to get the values you want. – DinoCoderSaurus May 3 '20 at 13:36

You must log in to answer this question.

Browse other questions tagged .