/main/insert_num/Django上的NoReverseMatch [英] NoReverseMatch at /main/insert_num/ Django

查看:116
本文介绍了/main/insert_num/Django上的NoReverseMatch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个django网络应用程序,该应用程序具有一种形式,要求用户输入电话号码并将该号码存储在postgres数据库中.以下代码给了我错误:

I'm trying to make to make a django web app which has a form that asks a user to input a phone number and stores that number in a postgres database. The following code is giving me the error:

在/main/insert_num/

NoReverseMatch at /main/insert_num/

找不到与"相反的字符. "不是有效的视图函数或模式名称.

Reverse for '' not found. '' is not a valid view function or pattern name.

我不知道问题出在哪里,有人可以帮忙吗?

And I can't figure out what the issue is, can someone help?

index.html

<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Test Form 1</title>
</head>
<body>
  <form action="{% url 'insert_my_num' %}" method="post" autocomplete="off">
    {% csrf_token %}
    <!-- {{ form.as_p }} -->
    <input type="submit" value="Send message">
  </form>
</body>
</html>

forms.py

from django import forms
from phone_field import PhoneField
from main.models import Post

class HomeForm(forms.ModelForm):
    phone = PhoneField()

    class Meta:
        model = Post
        fields = ('phone',)

models.py

from django.db import models
from phone_field import PhoneField


class Post(models.Model):
    phone = PhoneField()

main/urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('insert_num/', views.insert_my_num,name='insert_my_num')
]

project/urls.py

from django.contrib import admin
from django.urls import path,include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('main/',include('main.urls'))
]

views.py

def insert_my_num(request: HttpRequest):
    phone = Post(request.POST.get('phone'))
    phone.save()
    return redirect('')

推荐答案

您的views.py有点差-您不会在任何地方渲染表单.我起草了一个快速应用程序(我认为它可以满足您的需求)-让我知道它是否有效:

Your views.py is a little off - you aren't rendering your form anywhere. I drafted up a quick app (which I think does what you're looking for) - let me know if this works:

main/templates/index.html

在这里,我只是将表单的动作设置为""(这是您在这里所需要的全部),并取消了form.as_p行的注释

Here, I just set the form's action to "" (that's all you need here) and uncommented the form.as_p line

<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Test Form 1</title>
</head>
<body>
  <form action="" method="post" autocomplete="off">
    {% csrf_token %}
    {{ form.as_p }}
    <input type="submit" value="Send message">
  </form>
</body>
</html>

main/views.py

请注意这里的差异,我们正在测试请求类型,并根据传入的请求类型采取适当的措施.如果是POST请求,我们将处理表单数据并保存到数据库.如果没有,我们需要显示一个空白表格供用户填写.

Note the differences here, we are testing the request type and taking appropriate action based on what kind of request is coming in. If it's a POST request we process the form data and save to the database. If not, we need to display a blank form for the user to complete.

from django.shortcuts import render, redirect
from .forms import HomeForm


def insert_my_num(request):
    # Check if this is a POST request
    if request.method == 'POST':
        # Create an instance of HomeForm and populate with the request data
        form = HomeForm(request.POST)
        # Check if it is valid
        if form.is_valid():
            # Process the form data - here we're just saving to the database
            form.save()
            # Redirect back to the same view (normally you'd redirect to a success page or something)
            return redirect('insert_my_num')
    # If this isn't a POST request, create a blank form
    else:
        form = HomeForm()

    # Render the form
    return render(request, 'index.html', {'form': form})

让我知道是否可行!

这篇关于/main/insert_num/Django上的NoReverseMatch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆