Django urls() 对于以 10: 为基数的 int() 无效



我看过这篇文章没有成功。

我有一个评论网站,我正在尝试通过特定用户名发布所有评论,即".../评论/用户/用户名"。

urls.py:

urlpatterns = [
...
url(r'^reviews/user/(?P<username>w+)$', views.user_review_list, name='user_review_list'),
]

views.py(User_Name 是数据库中列的名称(:

def user_review_list(request, username):
latest_review_list = Review.objects.filter(User_Name=username).order_by('-Pub_Date')
context = {'latest_review_list': latest_review_list, 'User_Name': username}
return render(request, 'bourbons/user_review_list.html', context)

还有我称之为的html文件:

<h5>Rated {{ review.Rating }} of 5 by <u><a href="{% url 'app:user_review_list' review.User_Name %}" style="color:#ffffff">
{{ review.User_Name }}
</a></u></h5>

我发现了其他几个使用不同方法的帖子,但我仍然不断收到错误:"ValueError:int(( 的文字无效,基数为 10:'用户名'"。我觉得错误在 urls.py 部分甚至之前,因为我已经多次更改了 views.py 函数并且错误没有改变。

我正在使用 Django 版本 1.11.6。

谢谢

这是反馈中的额外内容...

主要 urls.py

from django.conf.urls import url, include
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
from . import views
urlpatterns = [
url(r'^$', views.home, name='home'),
url(r'^facts', views.facts, name='facts'),
url(r'^contact', views.contact, name='contact'),
url(r'^admin', include(admin.site.urls)),
url(r'^app/', include('app.urls', 
namespace='app')),
url(r'^accounts/', include('registration.backends.simple.urls')),
url(r'^accounts/', include('django.contrib.auth.urls', 
namespace="auth")),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

应用 urls.py

from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.bourbon_list, name='app_list'),
url(r'^app/(?P<ID>.+)$', views.app_detail, name='app_detail'),
url(r'^reviews$', views.review_list, name='review_list'),
url(r'^reviews/(?P<RatingID>.+)$', views.review_detail, name='review_detail'),
url(r'^reviews/user/(?P<username>w+)$', views.user_review_list, name='user_review_list'),
]

models.py

class Review(models.Model):
RATING_CHOICES = (
(1, '1'),
(2, '2'),
(3, '3'),
(4, '4'),
(5, '5'),)
RatingID = models.IntegerField(null=True, blank=True)
Item = models.ForeignKey(App)
Pub_Date = models.DateTimeField('date published')
User_Name = models.CharField(max_length=100)
Comment = models.CharField(max_length=200)
Rating = models.IntegerField(choices=RATING_CHOICES)

review_detail的 urlpattern 更改为

url(r'^reviews/(?P<RatingID>d+)$', views.review_detail, name='review_detail'),

正在发生的事情是,由于后续.+,此视图的现有网址模式正则表达式与reviews/下的任何其他网址匹配。

最新更新