未找到“sending_message”的反向。 'sending_message'不是有效的视图函数或模式名称

时间:2017-11-07 15:37:11

标签: python django

当我尝试访问view_user模板时,我有一个名为com的应用程序,其中包含一个带有操作的表单到另一个视图获取上面的错误

这是urls.py

app_name = 'com'
urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^(?P<name>[\w\-]+)/$',views.viewing_user, name='viewing_user'),

]

这是views.py

from django.shortcuts import render
from django.contrib.auth.models import User

# Create your views here.
RECEIVER_ID = 0
def index(request):
    return render(request, 'com/index.html',{})

def viewing_user(request, name):
    #username = request.GET.get('username','')
    try:
        User_obj = User.objects.get(username = name)
        RECEIVER_ID = User_obj.id
    except User.DoesNotExist:
        User_obj = None

    return render(request, 'com/viewing_user.html',{'u':name,'obj':User_obj})


def sending_message(request):
    form = MessageForm()
    if request.method == 'POST':
        form = MessageForm(request.POST)
        if form.is_valid:
            message = message_form.save(commit = False)
            message.date = datetime.date.now()
            message.from_user = user.id
            message._to = RECEIVER_ID
            message.save()

        else:
            print form.errors
    return render(request, 'com/viewing_user.html', {'form':form})

这是模板vieweing_user.html,似乎在表单

的操作中存在问题
<html>

  {% if obj == None %}

    <h2>Sorry this user ({{u}}) DoesNotExist</h2>

  {% else %}
    <h3>Be honest, and Tellme what you want</h3>
    <br>
      <i>{{obj.username}}</i>

      <form method="post" action="{%url 'com:sending_message' %}">
        {%csrf_token%}
        {% for hidden in form.hidden_fields%}
          {{hidden}}
        {%endfor%}

        {% for visible in form.visible_fields%}
        {{visible}}
        {%endfor%}

        <input type="submit" value='Tell'/>
      </form>

  {%endif%}

</html>

1 个答案:

答案 0 :(得分:0)

反向尝试在urlpatterns中查找其参数值。您在sending_message命名空间中没有名称为com的任何网址格式。

您可能希望创建名为sending_message

的网址格式

url(r'^(?Psomepattern)/$',views.sending_message, name='sending_message'),