Django 应用程序在本地运行,在部署到 DigitalOcean 时中断
Django app functions locally, breaks when deployed to DigitalOcean
这是我设置的 DigitalOcean droplet 上的错误:
Django 错误(第 38 行已突出显示)
Reverse for 'vendor' with arguments '()' and keyword arguments '{u'vendor': u''}' not found. 1 pattern(s) tried: [u'deals/vendor/(?P<vendor>[\w-]+)$']
...
34 {% if vendors %}
35 <li class="divider"></li>
36 <li class="dropdown-header">By Vendor</li>
37 {% for vendor_item in vendors|slice:"0:3" %}
38 <li><a href={% url 'deals:vendor' vendor=vendor_item.slug %}>{{ vendor_item.name }}</a></li>
39 {% endfor %}
40 {% endif %}
错误不是本地发生的。我看到关键字是一个空字符串,所以我去shell调查。
>>> from deals.models import Vendor
>>> vendor_qs = Vendor.objects.all()
>>> for item in vendor_qs: print item.slug
...
my-first-vendor
my-second-vendor
>>>
这是我要访问的页面的模型、视图和 url:
models.py
...
class Vendor(models.Model):
name = models.CharField(max_length=200)
slug = models.CharField(max_length=200, blank=True)
url = models.CharField(max_length=200, blank=True)
location = models.CharField(max_length=200, blank=True, null=True)
shipping = models.BooleanField(blank=True)
img = models.ImageField(blank=True)
def __str__(self):
return self.name
views.py
...
def index(request, data=None):
if data is None:
data = {}
vendors = Vendor.objects.annotate(Count('deal')) \
.order_by('name')
data['vendors'] = vendors
categories = Category.objects.annotate(Count('deal')) \
.exclude(deal__count__lt = 40) \
.order_by('name')
data['categories'] = categories
return render(request, 'deals/_index.html', data)
urls.py
urlpatterns = patterns('',
# Examples:
url(r'^$', views.index, name='index' ),
url(r'vendor/(?P<vendor>[\w-]+)$', views.get_deals_by_vendor, name='vendor'),
url(r'test_type/$', views.view_model_subclass_by_deal), # Remove in production
url(r'todays_deals/$', views.view_deals_updated_today), # Remove in production
)
同样,此代码的 100% 完全可以在本地找到。我花了大约 10 个小时进行故障排除,但我无法弄清楚...
注意:test_type 视图工作正常(只是一个输出其他模型子类的测试视图),而 todays_deals 不起作用。它returns:
render_to_string() got an unexpected keyword argument 'context'
它的视图如下所示:
def view_deals_updated_today(请求):
今天 = date.today()
todays_deals = Deal.objects \
.filter(updated_at__gte = today) \
.exclude(created_at__lte = today) \
.select_subclasses()
return render_to_response('test/deal_list.html', context = {'todays_deals':todays_deals})
我很困惑。即使 'pip freeze' 在这些环境之间也是相同的...
更新:
问题是 nginx 配置中的单个正斜杠。
配置的静态部分应该是“/static”而不是“/static/”
这是我设置的 DigitalOcean droplet 上的错误:
Django 错误(第 38 行已突出显示)
Reverse for 'vendor' with arguments '()' and keyword arguments '{u'vendor': u''}' not found. 1 pattern(s) tried: [u'deals/vendor/(?P<vendor>[\w-]+)$']
...
34 {% if vendors %}
35 <li class="divider"></li>
36 <li class="dropdown-header">By Vendor</li>
37 {% for vendor_item in vendors|slice:"0:3" %}
38 <li><a href={% url 'deals:vendor' vendor=vendor_item.slug %}>{{ vendor_item.name }}</a></li>
39 {% endfor %}
40 {% endif %}
错误不是本地发生的。我看到关键字是一个空字符串,所以我去shell调查。
>>> from deals.models import Vendor
>>> vendor_qs = Vendor.objects.all()
>>> for item in vendor_qs: print item.slug
...
my-first-vendor
my-second-vendor
>>>
这是我要访问的页面的模型、视图和 url:
models.py
...
class Vendor(models.Model):
name = models.CharField(max_length=200)
slug = models.CharField(max_length=200, blank=True)
url = models.CharField(max_length=200, blank=True)
location = models.CharField(max_length=200, blank=True, null=True)
shipping = models.BooleanField(blank=True)
img = models.ImageField(blank=True)
def __str__(self):
return self.name
views.py
...
def index(request, data=None):
if data is None:
data = {}
vendors = Vendor.objects.annotate(Count('deal')) \
.order_by('name')
data['vendors'] = vendors
categories = Category.objects.annotate(Count('deal')) \
.exclude(deal__count__lt = 40) \
.order_by('name')
data['categories'] = categories
return render(request, 'deals/_index.html', data)
urls.py
urlpatterns = patterns('',
# Examples:
url(r'^$', views.index, name='index' ),
url(r'vendor/(?P<vendor>[\w-]+)$', views.get_deals_by_vendor, name='vendor'),
url(r'test_type/$', views.view_model_subclass_by_deal), # Remove in production
url(r'todays_deals/$', views.view_deals_updated_today), # Remove in production
)
同样,此代码的 100% 完全可以在本地找到。我花了大约 10 个小时进行故障排除,但我无法弄清楚...
注意:test_type 视图工作正常(只是一个输出其他模型子类的测试视图),而 todays_deals 不起作用。它returns:
render_to_string() got an unexpected keyword argument 'context'
它的视图如下所示: def view_deals_updated_today(请求): 今天 = date.today()
todays_deals = Deal.objects \
.filter(updated_at__gte = today) \
.exclude(created_at__lte = today) \
.select_subclasses()
return render_to_response('test/deal_list.html', context = {'todays_deals':todays_deals})
我很困惑。即使 'pip freeze' 在这些环境之间也是相同的...
更新:
问题是 nginx 配置中的单个正斜杠。
配置的静态部分应该是“/static”而不是“/static/”