标签: django-urls

站点根页面的 Django 和 Nginx try_files 403

我对域使用这样的 Nginx 配置:

server_name_in_redirect off; 
listen 80;
server_name  ~^(www\.)?(.+)$;
root /var/www/$2/htdocs;

location / {
    try_files  $uri  $uri/ $uri/index.htm  @django;
    index index.html index.htm;
}

location @django {
    fastcgi_pass 127.0.0.1:8801;
    fastcgi_param PATH_INFO $fastcgi_script_name;
    fastcgi_param REQUEST_METHOD $request_method;
    fastcgi_param QUERY_STRING $query_string;
    fastcgi_param SERVER_NAME $server_name;
    fastcgi_param SERVER_PORT $server_port;
    fastcgi_param SERVER_PROTOCOL $server_protocol;
    fastcgi_param CONTENT_TYPE $content_type;
    fastcgi_param CONTENT_LENGTH $content_length;
    fastcgi_pass_header Authorization;
    fastcgi_intercept_errors off;
    fastcgi_param REMOTE_ADDR $remote_addr;
}
Run Code Online (Sandbox Code Playgroud)

Django URL 配置:

urlpatterns = patterns('',   
    url(r'^$', home, name='home'),
    url(r'index.htm', home, name='home'),    
    url(r'^(?P<name>.*).htm$', plain_page, name="plain_page"),
}
Run Code Online (Sandbox Code Playgroud)

所有像http://domain.com/somepage.htm这样的 url …

django nginx django-urls

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

/ 处的 NoReverseMatch,u'opts|admin_urlname' 不是已注册的命名空间 Django 1.4.1

这里是 Django 新手。根据文档,我尝试执行以下操作从我正在构建的公共网站的主页获取管理网站的链接:

{% load admin_urls %}
<p>Go to the <a href="{% url opts|admin_urlname:'add' %}">admin</a>.</p>
Run Code Online (Sandbox Code Playgroud)

我收到错误:

NoReverseMatch at /
u'opts|admin_urlname' is not a registered namespace
Run Code Online (Sandbox Code Playgroud)

我正确地包含了 URL:

    url(r'^admin/', include(admin.site.urls)),
Run Code Online (Sandbox Code Playgroud)

我的模板加载器的顺序正确

我对此尝试了几种不同的变体,它们都会引发命名空间错误。

有任何想法吗?谢谢!

django django-urls django-admin

3
推荐指数
2
解决办法
4094
查看次数

Django URL 映射:如何从 URL 路径中删除应用程序名称?

我有一个名为“myapp”的 Django 应用程序,我的索引页位于“myapp”目录中的 url.py 中,如下所示:

 url(r'^$', views.index, name='index')
Run Code Online (Sandbox Code Playgroud)

所以这本质上使测试服务器中的 URLhttp://127.0.0.1:8000/myapp/

然而,我不想要这个。http://127.0.0.1:8000/我希望每个网址都简单地从当前只能在测试服务器上访问的页面开始http://127.0.0.1:8000/myapp/bigduck,我想让它只能在http://127.0.0.1:8000/bigduck/

基本上,我只想从该应用程序的 url 映射方案中删除应用程序名称。

我怎样才能做到这一点?提前致谢。

python django django-urls

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

Django 1.7 中的 Url 包含空格时的 Url

我有一个应用程序,用户可以在其中创建事件。创建该事件后,就会有一个指向该事件的链接,该链接应该转到事件详细信息页面。

该 href 看起来像这样:

<a href="/events/view/{{event.event_name}}">Event Details </a> 
Run Code Online (Sandbox Code Playgroud)

因此示例 url 如下所示:

http://www.example.com/events/view/Food%20Drive%20LA
Run Code Online (Sandbox Code Playgroud)

%20 给我带来了问题,所以我无法渲染带有空格的网址。我认为我的事件 urls.py 中的正则表达式不正确:

url(r'^view/(?P<event_name>[\w%20+])$', views.event_details, name='event_details'),
Run Code Online (Sandbox Code Playgroud)

这是我的观点.py:

def event_details(request, event_name):
    event_name = event_name
    #...
    return render_to_response('events/event_details.html')
Run Code Online (Sandbox Code Playgroud)

这里可能有什么问题?

python django django-urls django-views

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

Django urls 调度程序错误 (urls.E004) 确保 urlpatterns 是 url() 实例的列表

我正在尝试将国际化添加到我的 Django 项目中。当我添加i18n_patterns()到我的主要内容时urls.py,我有以下问题:

ERRORS:
    ?: (urls.E004) Your URL pattern [<RegexURLResolver <module 'coffeebar.urls' from './coffee/coffeebar/urls.py'> (coffeebar:coffeebar) ^bar/>, <RegexURLResolver <RegexURLPattern list> (admin:admin) ^admin/>] is invalid. Ensure that urlpatterns is a list of url() instances.
Run Code Online (Sandbox Code Playgroud)

./coffee/coffee/urls.py(主网址配置)

from django.conf.urls import include, url
from django.conf.urls.i18n import i18n_patterns
from django.contrib import admin


urlpatterns = i18n_patterns([
    url(r'^bar/', include('coffeebar.urls')),
    url(r'^admin/', admin.site.urls),
])
Run Code Online (Sandbox Code Playgroud)

./coffee/coffeebar/urls.py(包含 urls 配置)

from django.conf.urls import url, include
from django.contrib.auth import views as auth_views

from . import views

app_name = …
Run Code Online (Sandbox Code Playgroud)

python django django-urls

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

Django- url 中的空字符串没有重定向到索引页

我正在学习 django,在我的应用程序中,我有一个 urls.py,其中有 4 个 url 模式。如果 url 末尾没有输入字符串,那么它必须根据代码重定向到索引页面,但它不会发生。其余网址工作正常。

我的网址.py

from django.contrib import admin
from django.urls import path
from  .views  import  index, about, news
from django.conf import settings
from mainsite import views

urlpatterns = [
    path(r'^$', views.index, name='index'),
    path(r'about/', views.about, name='about'),
    path(r'news/', views.news, name='news'),
    path(r'admin/', admin.site.urls),
]
Run Code Online (Sandbox Code Playgroud)

我的观点.py

from django.shortcuts import render
from datetime import datetime
from .models import Article

def index(request) :
    context = {
        'current_date': datetime.now(),
        'title': 'Home'
    }
    return render(request, 'index.html', context )

def about(request) :
    context …
Run Code Online (Sandbox Code Playgroud)

html python django django-urls django-views

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

类型错误:启动 django runserver 时“函数”类型的参数不可迭代

当我尝试运行服务器时,出现以下错误:

Exception in thread django-main-thread:
Traceback (most recent call last):
File "/Users/josephshenouda/anaconda3/lib/python3.7/threading.py", line 917, in _bootstrap_inner
self.run()
File "/Users/josephshenouda/anaconda3/lib/python3.7/threading.py", line 865, in run
self._target(*self._args, **self._kwargs)
File "/Users/josephshenouda/anaconda3/lib/python3.7/site-packages/django/utils/autoreload.py", line 54, in wrapper
fn(*args, **kwargs)
File "/Users/josephshenouda/anaconda3/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 117, in inner_run
self.check(display_num_errors=True)
File "/Users/josephshenouda/anaconda3/lib/python3.7/site-packages/django/core/management/base.py", line 390, in check
include_deployment_checks=include_deployment_checks,
File "/Users/josephshenouda/anaconda3/lib/python3.7/site-packages/django/core/management/base.py", line 377, in _run_checks
return checks.run_checks(**kwargs)
File "/Users/josephshenouda/anaconda3/lib/python3.7/site-packages/django/core/checks/registry.py", line 72, in run_checks
new_errors = check(app_configs=app_configs)
File "/Users/josephshenouda/anaconda3/lib/python3.7/site-packages/django/core/checks/urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "/Users/josephshenouda/anaconda3/lib/python3.7/site-packages/django/core/checks/urls.py", line 23, in check_resolver
return check_method() …
Run Code Online (Sandbox Code Playgroud)

python django django-urls

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

NoReverseMatch at / Reverse for 'single_product' 没有找到任何参数。尝试了 1 个模式:['products/(?P&lt;slug&gt;)/$']

我在使用 django 2.2 时遇到了这个错误,这是我的代码

网址.py

app_name = '产品'

urlpatterns = [
    url(r'^$', product_list, name='product-list'),
    url(r'^(?P<slug>.*)/$',single, name="single_product"),
    url(r'^category/(?P<slug>.*)/$',category_single,name="category")
]
Run Code Online (Sandbox Code Playgroud)

产品模型中的 views.py

def get_absolute_url(self,):
        return HttpResponseRedirect(reverse('single_product',args=[self.slug]))
Run Code Online (Sandbox Code Playgroud)

模板

<h3>{{ product }}</h3>
<p>{{ product.description }}</p>
<p>{{ product.get_price }}</p>
<p>
   <a href ="{% url 'products:single_product' %}" class = "btn btn-primary" role = "button">
      View Product
   </a>
Run Code Online (Sandbox Code Playgroud)

html python django django-urls

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

Django 删除尾部斜杠 - urls 或重定向

我正在尝试删除我的尾部斜杠,urls或者如果在末尾添加斜杠,则进行重定向。我尝试在 Internet 上的资源上执行此操作,但它对我不起作用。我想我做错了什么。

urls.py - 应用程序

app_name = 'shop'
urlpatterns = [
    # product
    path('', views.product_list, name='product_list'),
    path('show/<slug:slug>', views.product_show, name='product_show'),
    path('<slug:category>', views.product_list, name='lst_by_ctgry'),
    path('<slug:category>/<slug:subcategory>', views.product_list, name='lst_by_subctgry'),
    path('<slug:category>/<slug:subcategory>/<slug:kind>', views.product_list, name='lst_by_knds'),
    # info - these urls doesn't work without slashes. works only with slashes
    path('pad', views.pad, name='pad'),
    path('guarantee', views.guarantee, name='guarantee'),
    path('contacts', views.contacts, name='contacts'),
    path('about', views.about, name='about'),
    path('privacy', views.privacy, name='privacy')
]
Run Code Online (Sandbox Code Playgroud)

urls.py - 项目

urlpatterns = [
    path('', include('orders.urls')),
    path('', include('cart.urls')),
    path('', include('shop.urls')),
    path('admin/', admin.site.urls),
]
Run Code Online (Sandbox Code Playgroud)

设置.py

APPEND_SLASH = False
REMOVE_SLASH …
Run Code Online (Sandbox Code Playgroud)

django django-urls

3
推荐指数
2
解决办法
2707
查看次数

为什么 &lt;myproject&gt;/accounts/profile/ 显示 &lt;myproject&gt;/profile/ 页面

使用 django-allauth,成功登录后,用户被重定向到http://<myproject>/accounts/profile/...但是此 URL 不存在,但它仍然成功显示来自http://<myproject>/profile/

设置.py

urlpatterns = [
    path('', include('pages.urls')),
    path('admin/', admin.site.urls),
    url(r'^accounts/', include('allauth.urls')),
    url('album/', include('albums.urls')),
    url('profile/', include('user_profile.urls')),
]
Run Code Online (Sandbox Code Playgroud)

用户个人资料\urls.py

urlpatterns = [
    path('', views.profile, name='profile'),
]
Run Code Online (Sandbox Code Playgroud)

使用show_urls我没有看到任何/accounts/*可以调用view.profile视图的内容

/accounts/confirm-email/        allauth.account.views.EmailVerificationSentView account_email_verification_sent
/accounts/confirm-email/<key>/  allauth.account.views.ConfirmEmailView  account_confirm_email
/accounts/email/        allauth.account.views.EmailView account_email
/accounts/inactive/     allauth.account.views.AccountInactiveView       account_inactive
/accounts/login/        allauth.account.views.LoginView account_login
/accounts/logout/       allauth.account.views.LogoutView        account_logout
/accounts/password/change/      allauth.account.views.PasswordChangeView        account_change_password
/accounts/password/reset/       allauth.account.views.PasswordResetView account_reset_password
/accounts/password/reset/done/  allauth.account.views.PasswordResetDoneView     account_reset_password_done
/accounts/password/reset/key/<uidb36>-<key>/    allauth.account.views.PasswordResetFromKeyView  account_reset_password_from_key
/accounts/password/reset/key/done/      allauth.account.views.PasswordResetFromKeyDoneView      account_reset_password_from_key_done
/accounts/password/set/ allauth.account.views.PasswordSetView   account_set_password
/accounts/signup/       allauth.account.views.SignupView        account_signup
/accounts/social/connections/   allauth.socialaccount.views.ConnectionsView     socialaccount_connections
/accounts/social/login/cancelled/ …
Run Code Online (Sandbox Code Playgroud)

django django-urls django-allauth

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