我有一个看起来像这样的 Django 模型:
class Person(models.Model):
name = models.CharField(max_length=32)
place = models.ForeignKey(Place, related_name='people')
approved = models.BooleanField()
objects = PersonManager()
@models.permalink
def get_absolute_url(self):
return('deal_details', (), {
'person_slug': slugify(self.name),
})
Run Code Online (Sandbox Code Playgroud)
如您所见,我已经有了对象的绝对 URL。但是,我想创建一个难以猜测的 URL 来跟踪对象的批准过程。有人做过类似的事情和/或对我应该如何进行有任何建议吗?
我的第一个想法是创建一个像obfuscated_key这样的模型字段,它是通过save模型的函数随机生成的。然后 URL 看起来像/people/status/<id>/<obfuscated_key>/. 但也许有更好的方法来解决这个问题?
我有一个像这样的Django URL:
url(r'^(?P<category_id>\w+)/beginner/$', views.beginner, name='beginner')
Run Code Online (Sandbox Code Playgroud)
这个正则表达式接受这样的URL:
http://127.0.0.1:8000/quiz/grammar/beginner/
http://127.0.0.1:8000/quiz/vocabulary/beginner/
Run Code Online (Sandbox Code Playgroud)
但是'-'不接受URL :
# This URL is not accepted
http://127.0.0.1:8000/quiz/business-english/beginner/
Run Code Online (Sandbox Code Playgroud)
如何修改此正则表达式以接受URL '-'
在 Django 中是否有必要命名用于 url 模板标签的 url?
文档中给出了以下 url 示例:
('^client/([0-9]+)/$', 'app_views.client', name='app-views-client')
Run Code Online (Sandbox Code Playgroud)
并且可以在这样的模板中使用:
{% url 'app-views-client' client.id %}
Run Code Online (Sandbox Code Playgroud)
但是有没有办法通过路径而不是名称来反转视图?有一种方法在 1.8 中已弃用,并将在 Django 2.0 中删除,如下所示:
{% url 'path.to.some_view' v1 %}
Run Code Online (Sandbox Code Playgroud)
在 1.8 及更高版本中,是否还有一种方法可以在不命名的情况下反转视图?
我有一个表单供用户填写 <home>/data。但是,我希望用户只有在同意位于 < home >/terms 的某些条款时才能访问该表单。
有没有办法确保访问 <home>/data 的唯一方法是从 <home>/terms 上的同意按钮?即在地址栏中输入 <home>/data 应该会抛出错误或重定向到条款。
万一重要,我不存储用户(我使用会话数据)。
谢谢!
I am trying to include a Django sub-app's urls into the main urls.py.
app/urls.py:
urlpatterns = patterns(
'',
...
include('transfers.urls'),
)
Run Code Online (Sandbox Code Playgroud)
app/transfers/urls.py:
urlpatterns = patterns(
'',
url(r'^transfers/$', 'some.view'),
...
)
Run Code Online (Sandbox Code Playgroud)
But I get a route not found error. The last element of the route is just the URLconf module from the sub-app. It has not been delisted into the parent URL list.
Using the URLconf defined in app.urls, Django tried these URL patterns, in this order:
1.
2.
...
30. …Run Code Online (Sandbox Code Playgroud) 我最近开始使用Django 1.9 for python开发.我也是python的新手.只需通过示例和代码来学习东西.我遇到了包括在django.conf.urls,当我用导致的错误.我不明白为什么会这样?因为我在其他不会导致错误的地方使用过它.
from django.conf.urls import url, include
from accounts import views as acc_views
urlpatterns = [
url(r'^home$', acc_views.home, name='accounts_home'),
]
Run Code Online (Sandbox Code Playgroud)
下面是这给出错误的时候.
urlpatterns = [
url(r'^home$', include(acc_views.home), name='accounts_home'),
]
Run Code Online (Sandbox Code Playgroud)
这是一个例外:
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x035424F8>
Traceback (most recent call last):
File "C:\Python35-32\VirtualENV\socialnetwork296\lib\site-packages\django\core\urlresolvers.py", line 419, in url_patterns
iter(patterns)
TypeError: 'function' object is not iterable
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Python35-32\VirtualENV\socialnetwork296\lib\site-packages\django\utils\autoreload.py", line …Run Code Online (Sandbox Code Playgroud) 我已经看到了一堆关于这个的问题,但还没有找到答案。
这是我的模型:
class UserProfile(models.Model):
user = models.OneToOneField(User)
.
.
.
website = models.URLField(max_length=100, blank=True, null=True)
Run Code Online (Sandbox Code Playgroud)
和我的forms.py:
class UserProfileForm(forms.ModelForm):
class Meta:
model = UserProfile
fields = ('website')
def clean_website(self):
website = self.cleaned_data['website']
if website and not website.startswith('http://'):
website = 'http://' + website
return website
# def clean(self):
# cleaned_data = self.cleaned_data
# url = cleaned_data.get('url')
#
# if url and not url.startswith('http://'):
# url = 'http://' + url
# cleaned_data['url'] = url
# return cleaned_data
Run Code Online (Sandbox Code Playgroud)
我试图清理网址,但我的 django 设置方式我没有机会获得清理功能。

如何更改 Django 以允许用户不输入 …
我正在Django创建一个分类广告网站.单一视图功能可处理全球列表,城市列表,仅限易货的全球列表以及仅限易货的城市列表.这个观点叫做ads.
url模式按以下顺序编写(请注意,每个模式都有一个唯一的名称,尽管它与同一ads视图相关联):
urlpatterns = patterns('',
url(r'^buy_and_sell/$', ads,name='classified_listing'),
url(r'^buy_and_sell/barter/$', ads,name='barter_classified_listing'),
url(r'^buy_and_sell/barter/(?P<city>[\w.@+-]+)/$', ads,name='city_barter_classified_listing'),
url(r'^buy_and_sell/(?P<city>[\w.@+-]+)/$', ads,name='city_classified_listing'),
)
Run Code Online (Sandbox Code Playgroud)
问题是,当我点击classified_listing上面列表中命名的url时,函数ads会被调用两次.也就是我在终端中看到的内容:
[14/Jul/2017 14:31:08] "GET /buy_and_sell/ HTTP/1.1" 200 53758
[14/Jul/2017 14:31:08] "GET /buy_and_sell/None/ HTTP/1.1" 200 32882
Run Code Online (Sandbox Code Playgroud)
这意味着加倍处理.我认为urls.py返回匹配的第一个url模式.我做错了什么,解决这个问题的最佳方法是什么?所有其他调用按预期btw工作(即仅一次).
注意:如果我错过了什么,请询问更多信息.
了解这些类型的出现的很好的解释:https://groups.google.com/d/msg/django-users/CRMMYWix_60/KEIkguUcqxYJ
我是Django的新手并尝试制作一个项目,但我面临一个简单的问题.我正在Django 2中为root编写一条路径,它不起作用,但对于其他东西,它可以工作.任何人都可以指出它为什么不起作用.
工作原理:
path(r'home/', home, name='home'),
Run Code Online (Sandbox Code Playgroud)
这不起作用:
path(r'^$', home, name='home'),
Run Code Online (Sandbox Code Playgroud)
而且要明确一点:我没有加载这两条线.我一次评论一行,所以没有订单问题.
我有 Django 的问题。实际上,我想在我的新站点中创建一个登录面板,但是当我尝试写入地址时,调试器会向我响应一个错误:
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/account/login
Using the URLconf defined in firmowa.urls, Django tried these URL patterns, in this order:
admin/
^account/
The current path, account/login, didn't match any of these.
Run Code Online (Sandbox Code Playgroud)
我的firmowa.urls是
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path(r'^account/', include('account.urls')),
]
Run Code Online (Sandbox Code Playgroud)
项目 urls.py 是:
from django.urls import path
from . import views
urlpatterns = [
path(r'^login/$', views.user_login, name='login'),
]
Run Code Online (Sandbox Code Playgroud)
我正在寻找几个小时的解决方案,但仍然一无所获。
django ×10
django-urls ×10
python ×5
django-2.0 ×2
django-views ×2
django-apps ×1
django-forms ×1
obfuscation ×1
python-3.x ×1
regex ×1
url-routing ×1