标签: django-urls

Django:如何传递变量以包含 url 标签中的标签?

所以现在我硬编码到 url,如果你移动端点,这会有点烦人。这是我当前的导航栏项目设置。

# in base.html

{% include 'components/navbar/nav-item.html' with title='Event Manager' url='/eventmanager/' %}
Run Code Online (Sandbox Code Playgroud)
# in components/navbar/nav-item.html

<li>
  <a href="{{ url }}">{{ title }}</a>
</li>
Run Code Online (Sandbox Code Playgroud)

看看我现在如何使用该网址?

我现在想要的是这样的:

{% include 'components/navbar/link.html' with title='Event Manager' url={% url 'event_manager:index' %} %}
Run Code Online (Sandbox Code Playgroud)

但显然,这是无效的语法。我该怎么做?

如果不可能,我如何创建一个应用程序,以某种方式创建一个视图,我可以在其中传递带有上下文变量的所有 URL?从理论上讲,这听起来很简单,但我必须以某种方式将该视图插入到所有其他视图中。

python django django-templates django-urls django-views

1
推荐指数
1
解决办法
819
查看次数

视图 main.views.home 未返回 HttpResponse 对象。它返回 None 相反

好吧,我浏览了一些关于这个 ValueError 的不同的松弛帖子,但似乎大多数都与不返回渲染有关,看起来我做得正确......?

我确信这与我的 if 语句有关,只是不确定到底是什么或如何正确设置代码,以便我可以检查对浏览器的表单请求。

编辑:根据评论,我暂时检查了 is_valid 只是为了看看是否会收到新的错误,而且似乎我收到了名称错误。“名称‘名称’未定义”

所以它无法将用户输入的表单获取到 api 中。

views.py:

from http.client import responses
from django.shortcuts import render
from .forms import SearchUser
from .search import search


def home(request):
    if request.method == "POST":
        form = SearchUser(request.POST)
        form.cleaned_data["name"]
    else:
            return render(request, "main/home.html", {
                'form': SearchUser(),  # Reference to form
                'userid': search(request),
                # 'mmr':NA,
            })
Run Code Online (Sandbox Code Playgroud)

search.py:

import requests


def search(request):
    data = requests.get(
        f"https://americas.api.riotgames.com/riot/account/v1/accounts/by-riot-id/{name}/NA1?api_key=RGAPI-d1224a2c-9130-45ff-8c05-0656d56d105f")
    return data.json()['puuid']
Run Code Online (Sandbox Code Playgroud)

urls.py:

from django.urls import path
from . import views


urlpatterns = [
    path("", views.home, …
Run Code Online (Sandbox Code Playgroud)

python django django-urls django-forms django-views

1
推荐指数
1
解决办法
5049
查看次数

Slug字段后跟url

我刚开始使用Django和Python,所以我还是新手.这是我的urls.py:

url(r'(?P<slug>[-\w]+)/$','person_detail'),
url(r'(?P<slug>[-\w]+)/delete/$','person_delete'),
Run Code Online (Sandbox Code Playgroud)

问题是,当我尝试对url执行:slug/delete /它正在寻找整个部分slug/delete /作为slug.当我删除第一个url中的$时,它不会转到person_delete视图,而是转到person_detail视图,忽略/ delete/part任何想法?

django url django-urls

0
推荐指数
1
解决办法
4638
查看次数

为什么我的urls.py没有从模块中拉出来?

我的Django urls.py有:

from django.conf.urls.defaults import patterns, include, url
import admin
import settings
import pim_calendar
import pim_scratchpad
import pim.views

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    url(r'^$', pim.views.home, name = 'home'),
    url(r'^save_calendar$', pim_calendar.views.save_calendar, name =
      'save_calendar'),
    url(r'^save_scratchpad$', pim_scratchpad.views.save_scratchpad, name =
      'save_scratchpad'),
    url(r'^view_calendar$', pim_calendar.views.view_calendar, name =
      'view_calendar'),
    url(r'^view_scratchpad$', pim_scratchpad.views.view_scratchpad, name =
      'view_scratchpad'),

    url(r'^accounts/login/$', 'django.contrib.auth.views.login'),
    # Uncomment the admin/doc line below to enable admin documentation:
    url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the …
Run Code Online (Sandbox Code Playgroud)

python django django-urls django-views

0
推荐指数
1
解决办法
4601
查看次数

django重定向不重定向

我在视图中有以下形式逻辑:

if request.method == 'POST':
        form = MyForm(request.POST, request.FILES)
        if form.is_valid():

            my_form = form.save()                                          )
            print 'before redirect'
            redirect('customer:department-edit')
            print 'after redirect'
Run Code Online (Sandbox Code Playgroud)

我的网址条目如下所示:

url(r'^departments/$', views.departments_view, name='department-edit'),
Run Code Online (Sandbox Code Playgroud)

我得到以下输出:

before redirect
after redirect
Run Code Online (Sandbox Code Playgroud)

为什么在提交表单后不会发生重定向?

django redirect django-urls django-forms

0
推荐指数
1
解决办法
167
查看次数

Django命名组不匹配

这些是我的文件:

urls.py:

from django.conf.urls import patterns, include, url
from eiris_wipro.views import *   

urlpatterns = patterns('',
                   (r'^hello/$',hello),
                   (r'^articles/(?P<collection>)/$', restusers),
)
Run Code Online (Sandbox Code Playgroud)

views.py: 来自django.http导入HttpResponse

def hello(request):
    return HttpResponse("Hello new world!")

def restusers(request, collection='smthn'):
    print 'Collection', collection
    return HttpResponse(collection)
Run Code Online (Sandbox Code Playgroud)

当我尝试击球时http://127.0.0.1:8000/articles/smthn/,我收到404错误!!

我应该遗漏一些非常基本的东西.可能是什么?

python django django-urls

0
推荐指数
1
解决办法
76
查看次数

在“?”之后获取数据 在网址,django模板中。

我有以下格式的网址。

http://127.0.0.1:8000/accounts/login/?next=/event/contract-risk-management/review/

我需要从模板中“解析”“ / event / contract-risk-management / review /”部分。问题是我不知道如何在问号后得到零件。

我尝试了request.path,但它只返回了网址的第一部分。(无域)。

有谁知道我该怎么用?谢谢。

django django-templates django-urls

0
推荐指数
1
解决办法
975
查看次数

如何使用字符串转到Django视图中的URL?

在我的模板中,我有一个带有id的DOM元素.

<h1 id="club-title">{{club.title}}</h1>
Run Code Online (Sandbox Code Playgroud)

像这样的东西.

这是一个名为club_detail的视图.此视图的网址位于urls.py中:

url(r'^club/(?P<pk>[0-9]+)/$', views.club_detail, name='club_detail'),
Run Code Online (Sandbox Code Playgroud)

在我的一个观点中,我想转到上面的url,但我还想在URL的末尾添加"#club-title",以便浏览器向下滚动到我的元素.我该怎么做呢?

目前,视图看起来像这样:

def index(request):
    .....
    return redirect('myapp:club_detail', pk = str(club.pk))
Run Code Online (Sandbox Code Playgroud)

我希望我很清楚.谢谢!

python django django-urls django-views

0
推荐指数
1
解决办法
808
查看次数

Django URL持续时间更长

我是django的新手,我正在研究url重定向.在我的观点我使用HttpResponseRedirect和渲染函数来切换视图.

问题是在视图之间进行几次切换后,URL会越来越长.这是我在addcostumer和delete costumer视图之间切换后的chrome上的URL

http://127.0.0.1:8000/interface/addcostumer/deletecostumer/addcostumer/deletecostumer/preview/preview/addcostumer/deletecostumer/
Run Code Online (Sandbox Code Playgroud)

如何制作网址

http://127.0.0.1:8000/interface/addcostumer/
Run Code Online (Sandbox Code Playgroud)

当我在addcostumer视图和

http://127.0.0.1:8000/interface/deletecostumer/
Run Code Online (Sandbox Code Playgroud)

当我在deletecostumer视图中

当我在视图之间切换时,他们会一个接一个地附加它们.

class AddCostumerView(FirstPageView):

def __init__(self):
    super()
    self.main_template = "addcostumer.html"

def get(self, request):
    form = CostumerForm()
    return render(request, template_name=self.base_template,
                  context={"company_list": list(database.get_companies()),
                           "sister_page": self.main_template,
                           "form": form})

@method_decorator(csrf_protect)
def post(self, request):
    print("this line is running now")
    form = CostumerForm(request.POST)

    if request.FILES["myfile"]:
        myfile = request.FILES["myfile"]
        fs = FileSystemStorage()
        filename = fs.save(myfile.name, myfile)


    if form.is_valid():
        data = form.cleaned_data
        #database.add_costumer(**data)

        #preview the data and preview the file

        #Add for final submission
        #messages.success(request, filename)

        return HttpResponseRedirect("preview/")  # TODO …
Run Code Online (Sandbox Code Playgroud)

python django django-urls django-views

0
推荐指数
1
解决办法
116
查看次数

Django将已知的精确字符串作为url参数传递

我在调度员中有两个网址指向相同的视图

path('posts/top/', posts, name='top'),
path('posts/new/', posts, name='new'),
Run Code Online (Sandbox Code Playgroud)

我希望查看开始如下:

def posts(request, ordering):
    ...
Run Code Online (Sandbox Code Playgroud)

我想,传递topnew作为参数应该是这样的:

path('posts/<ordering:top>/', posts, name='top'),
path('posts/<ordering:new>/', posts, name='new'),
Run Code Online (Sandbox Code Playgroud)

但它给了我:

django.core.exceptions.ImproperlyConfigured: URL route 'posts/<ordering:top>/' uses invalid converter 'ordering'.
Run Code Online (Sandbox Code Playgroud)

所以,作为一个解决方法我使用它,但它看起来有点脏:

path('posts/top/', posts, name='top', kwargs={'order': 'top'}),
path('posts/new/', posts, name='new', kwargs={'order': 'top'}),
Run Code Online (Sandbox Code Playgroud)

做正确的方法是什么?

django django-urls

0
推荐指数
1
解决办法
777
查看次数