Arpana Mehta
Arpana Mehta

Reputation: 23

Django view function not being called on click of submit

I have two view functions in my views.py. The first one renders the index.html
Second is created for the form action on index.html (update_db).
When I click on submit on the index.html file, it changes url to /update1, but the function call has print('HI') and I cannot see that on console. Neither are any new files created after it runs.

Intially I had return render(request, 'index.html', {} ) but I am not sure if that should be returned. Is there some problem with my urls.py?

views.py

from django.shortcuts import render
from django.conf import settings
from .models import Image, Profile
import random
# Create your views here.
def index(request):

    y= random.randint(0,10)
    for i in range(0,10):
        pass 
    p = Image.objects.all()[y]
    print(p.p_img)
    count = p.p_img_count
    lst = [p.s_image1, p.s_image2, p.s_image3, p.s_image3, p.s_image4, p.s_image5]
    tmp =[]
    for i in range(0,3):
        px = Image.objects.all()[random.randint(0,14)]
        tmps=[px.s_image1, px.s_image2, px.s_image3, px.s_image3, px.s_image4, px.s_image5]
        tmp.append(tmps[random.randint(0,4)])

    x = random.randint(0,4)
    s_img = [lst[x]] + tmp
    random.shuffle(s_img)
    print('hevfxy')
    print(p.p_img)

    return render(request,'index.html',{"p_img":p, "s_img": s_img, 'media_url':settings.MEDIA_URL})

def update_db(request):

    print('HI')
    # username = request.user.username
    user = request.POST.get("user")
    p_img = request.POST.get("p_img")
    ans = request.POST.get("ans")

    y = Image.objects.get(p_img=p_img).id
    y=y-8  
    Profile.objects.create(user=user, p_img_id=y, p_ans=ans)

    return something

urls.py (main conf)

from django.conf.urls import url, include
from django.urls import path
from django.contrib import admin
from django.contrib.auth import views as auth_views
from django.conf.urls.static import static
from django.conf import settings

urlpatterns = static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + [
    url(r'^admin/', admin.site.urls),
    url(r'^login/$', auth_views.LoginView.as_view(template_name="login.html"), name="login"),
    url(r'^logout/$', auth_views.LogoutView.as_view(template_name="logout.html"), {'next_page': '/'}, name='logout'),
    url('^', include('pair.urls')),


] 

urls.py

from django.conf.urls import url
from django.urls import path

from . import views
app_name ='pair'

urlpatterns = [
    url('/update1', views.update_db, name='update_db'),
    url('$', views.index, name='index'),

]

index.html

<form action="/update1" method="POST" >

    {% csrf_token %}

  Player : <b> {{ user.get_username }} </b> 
<p style="background-color: #ddd674">Primary image :
    <input type="hidden" name="user" id="{{ user.get_username }}" value="{{ user.get_username }}"></input>
    <input type="hidden" name="p_img" id="{{ p_img.p_img }}" value="{{ p_img.p_img }}"><img id="p_img" src="{{media_url}}{{ p_img.p_img }}" height=300px ></input>

</p>
<hr height=2px >
<p style="background-color: #a7e0d9">Secondary image :

    {% for img in s_img %}
        {% if img  %}
            <input type="radio" id="{{ img }}" name="ans" value="{{ img }}" >
                <img src="{{media_url}}{{ img }}" height=250px > 
            </input>
        {% endif %}
    {% endfor %}
    <br> 

</p>
<hr>
{{ ans }}

<input type="submit" value="Submit" />
  </form>

Upvotes: 2

Views: 1500

Answers (2)

Atr0x
Atr0x

Reputation: 196

I think your URL is referenced at "localhost:8000//update1" instead of "localhost:8000/update1"

Just change from this:

url('/update1', views.update_db, name='update_db')

to that:

url('update1', views.update_db, name='update_db')

Upvotes: 0

engin_ipek
engin_ipek

Reputation: 917

You can add any number of methods in your views.py. Unless they return a rendered html file, I dont assume django will consider them as views.

My two bets are:

  1. Change url('/update1', views.update_db.as_wiew(), name='update_db'),

  2. Make your update_db method to return something like return render(something).

Hope to hear from you!

Upvotes: -1

Related Questions