单击提交时未调用 Django 视图函数

2024-02-14

我的views.py中有两个视图函数。第一个呈现index.html
第二个是为 index.html (update_db) 上的表单操作创建的。
当我单击 index.html 文件上的“提交”时,它将 url 更改为 /update1,但函数调用具有 print('HI') 并且我在控制台上看不到它。运行后也不会创建任何新文件。

最初我有 return render(request, 'index.html', {} ) 但我不确定是否应该返回。我的 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(主要会议)

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'),

]

索引.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>


我认为您的 URL 被引用为“localhost:8000//update1”而不是“localhost:8000/update1”

只需从此更改:

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

to that:

url('update1', views.update_db, name='update_db')
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

单击提交时未调用 Django 视图函数 的相关文章

随机推荐