从请求中获取URL

时间:2022-02-25 16:52:53

Here's my urls.py:

这是我的urls.py:

from django.conf.urls import include, url
from django.contrib import admin
from stories.views import check, post

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url('^.*/$', check),
]

And here's my views.py:

这是我的views.py:

from django.http import HttpResponse
from django.shortcuts import render
from datetime import datetime
from models import Story

from .forms import StoryForm

def check(request):
    try:
        existing_story = Story.objects.get(name=?URLRequested?)
    except Story.DoesNotExist:
        return HttpResponse(post(request))

    HttpResponse(existing_story.text)

I want to convert the url entered that redirects the 404 and use it as the name for a new object. How can this be done?

我想转换输入的网址,重定向404并将其用作新对象的名称。如何才能做到这一点?

1 个解决方案

#1


0  

The easiest and most common way would be to make a parameter in your url, although .* is a very broad regex expression

最简单和最常见的方法是在你的网址中创建一个参数,尽管。*是一个非常广泛的正则表达式

url('^(?P<parameter_name>.*)/$', check),

def check(request, parameter_name):    
    try:
        existing_story = Story.objects.get(name=parameter_name)

More commonly is to look for a specific match such as \w+ to look for a string of text of a name or similar.

更常见的是寻找特定匹配,例如\ w +,以查找名称或类似文本的字符串。

See the documentation for more information

有关更多信息,请参阅文档

#1


0  

The easiest and most common way would be to make a parameter in your url, although .* is a very broad regex expression

最简单和最常见的方法是在你的网址中创建一个参数,尽管。*是一个非常广泛的正则表达式

url('^(?P<parameter_name>.*)/$', check),

def check(request, parameter_name):    
    try:
        existing_story = Story.objects.get(name=parameter_name)

More commonly is to look for a specific match such as \w+ to look for a string of text of a name or similar.

更常见的是寻找特定匹配,例如\ w +,以查找名称或类似文本的字符串。

See the documentation for more information

有关更多信息,请参阅文档