模板

时间:2015-10-06 21:24:31

标签: django django-templates django-views

它应该是简单的东西,但我花了最后两个小时试图解决这个问题。这是在Django 1.7和Python 2.7

我收到以下错误:

ImproperlyConfigured at /

The included urlconf 'blog.urls' does not appear to have any patterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import.

由模板中的这一行引起:

>> {% url 'referrals:confirm_referral' claimCode %} <<

在视图中执行以下行时:

msg_plain = render_to_string('referrals/email/sendInvite.txt', emailVariables)

我在dict中尝试reverse_lazy调用,以及懒惰,但这还没有解决问题。

这是我的urls.py:

urlpatterns = patterns('',
    url(r'^confirm-referral/<confirm_code>[a-zA-Z0-9]{8}$', views.confirmReferral, name='confirm_referral', ),
)

这是代码的相关部分(在一个名为jobs.py的单独文件中):

            emailVariables = {
                'commsPromoHeadline' : thisOfferClaim.linkedOffer.commsPromoHeadline,
                'commsPromoSubHeading' : thisOfferClaim.linkedOffer.commsPromoSubHeading,
                'commsInvitationShortMessage' : thisOfferClaim.linkedOffer.commsInvitationShortMessage,
                'commsInvitationLongMessage' : thisOfferClaim.linkedOffer.commsInvitationLongMessage,
                'commsOfferButtonText' : thisOfferClaim.linkedOffer.commsOfferButtonText,
                'commsBanner' : thisOfferClaim.linkedOffer.commsBanner,
                'claimCode' : thisRecipient.claimCode, 
                #'offerLink' : lazy('referrals:confirm_referral' + thisRecipient.claimCode), 
                #'offerLink' : reverse_lazy('referrals:confirm_referral'), 
                'sender': thisOfferClaim.sender,
                'recipient' : thisRecipient
                }
            msg_plain = render_to_string('referrals/email/sendInvite.txt', emailVariables)
            msg_html = render_to_string('referrals/email/sendInvite.html', emailVariables)

最后,以下是相关模板的摘录:

You can quickly join by clicking on the link below:
>> {% url 'referrals:confirm_referral' claimCode%} <<

我做错了什么?

更新1: 我在上面的词典中注释了两行。 当我取消注释时

offerLink' : reverse_lazy('referrals:confirm_referral'), 

我将以下内容作为返回值:

<django.utils.functional.__proxy__ object at 0x0441F890>

我原本期待一个字符串?这是导致问题的原因吗?

更新2: 我的主要urls.py如下:

import os
from django.views.generic import TemplateView
from django.conf.urls import patterns, include, url
from django.conf.urls.static import static
from django.conf import settings
from django.contrib import admin

from wagtail.wagtailadmin import urls as wagtailadmin_urls
from wagtail.wagtailsearch import urls as wagtailsearch_urls
from wagtail.wagtaildocs import urls as wagtaildocs_urls
from wagtail.wagtailcore import urls as wagtail_urls
from wagtail.contrib.wagtailsitemaps.views import sitemap
#import debug_toolbar


urlpatterns = patterns('',
    #url(r'^__debug__/', include(debug_toolbar.urls)),
    url('^$', TemplateView.as_view(template_name='home.html')),
    url(r'^', include('people.urls')),
    url(r'^', include('reviews.urls')),
    url(r'^', include('commerce.urls')),
    url(r'^accounts/', include('allauth.urls')),
    url(r'^activities/', include('video.urls',namespace='activities')),
    url(r'^course/', include('video.urls',namespace='course-videos')),
    url(r'^refer/', include('referrals.urls',namespace='referrals')),
    url(r'^robots.txt$', TemplateView.as_view(template_name='robots.txt', content_type='text/plain')),
    url(r'^', include('people.urls')),
    url(r'^mission-control/', include(admin.site.urls)),
    url(r'^cms-admin/', include(wagtailadmin_urls)),
    url(r'^search/', include(wagtailsearch_urls)),
    url(r'^documents/', include(wagtaildocs_urls)),
    url('^sitemap\.xml$', sitemap),
    url(r'^', include(wagtail_urls)),
)


if settings.DEBUG:
    from django.contrib.staticfiles.urls import staticfiles_urlpatterns


    urlpatterns += staticfiles_urlpatterns()
    urlpatterns += static(settings.MEDIA_URL + 'video/', document_root=os.path.join(settings.MEDIA_ROOT, 'video'))
    urlpatterns += static(settings.MEDIA_URL + 'images/', document_root=os.path.join(settings.MEDIA_ROOT, 'images'))
    urlpatterns += static(settings.MEDIA_URL + 'attachments/', document_root=os.path.join(settings.MEDIA_ROOT, 'attachments'))
    urlpatterns += static(settings.MEDIA_URL + 'platform/', document_root=os.path.join(settings.MEDIA_ROOT, 'platform'))
    urlpatterns += static(settings.MEDIA_URL + 'offer/', document_root=os.path.join(settings.MEDIA_ROOT, 'offer'))
    urlpatterns += static(settings.MEDIA_URL + 'people/', document_root=os.path.join(settings.MEDIA_ROOT, 'people'))
    urlpatterns += static(settings.MEDIA_URL + 'reward/', document_root=os.path.join(settings.MEDIA_ROOT, 'reward'))

更新3: 我已将所有内容都移到视图中,但我仍然遇到同样的错误。代码可以在下面找到:

class sendReferralMail():
    ##########################################################
    # this job sends out emails who have been invited to 
    # participate in a referral scheme
    ##########################################################
    try:
        #try to get all outstanding claims that are linked to receipients with a status of "commsPending". also prefetch recipient details to minimise DB hits
        allOfferClaims = OfferClaim.objects.filter(recipient__status='commsPending').prefetch_related('recipient')
    except:
        allOfferClaims = None
    if allOfferClaims:
        for thisOfferClaim in allOfferClaims:
            #go through each recipient that we've already filtered on previously
            for thisRecipient in thisOfferClaim.recipient.all():
                if thisRecipient.status == 'commsPending':
                    #get offer-specific content that we should be sending them
                    emailVariables = {
                        'commsPromoHeadline' : thisOfferClaim.linkedOffer.commsPromoHeadline,
                        'commsPromoSubHeading' : thisOfferClaim.linkedOffer.commsPromoSubHeading,
                        'commsInvitationShortMessage' : thisOfferClaim.linkedOffer.commsInvitationShortMessage,
                        'commsInvitationLongMessage' : thisOfferClaim.linkedOffer.commsInvitationLongMessage,
                        'commsOfferButtonText' : thisOfferClaim.linkedOffer.commsOfferButtonText,
                        'commsBanner' : thisOfferClaim.linkedOffer.commsBanner,
                        'claimCode' : thisRecipient.claimCode, 
                        'domain' : Site.objects.get_current().domain,
                        'offerLink' : unicode(reverse_lazy('referrals:confirm_referral')), 
                        'full_url' : ''.join(['http://', Site.objects.get_current().domain, unicode(reverse_lazy('referrals:confirm_referral')), thisRecipient.claimCode]),
                        'sender': thisOfferClaim.sender,
                        'recipient' : thisRecipient
                        }
                    msg_plain = render_to_string('referrals/email/sendInvite.txt', emailVariables)
                    msg_html = render_to_string('referrals/email/sendInvite.html', emailVariables)
                    send_mail(
                        thisOfferClaim.linkedOffer.commsPromoHeadline,
                        msg_plain,
                        settings.REFERRAL_EMAIL_SENDER,
                        [thisRecipient.email],
                        html_message=msg_html,
                    )

2 个答案:

答案 0 :(得分:0)

您指定的正则表达式组格式不正确,应该是(?P<name>pattern)格式:

r'^confirm-referral/<confirm_code>[a-zA-Z0-9]{8}$'

应该成为:

r'^confirm-referral/(?P<confirm_code>[a-zA-Z0-9]{8})$'

其中:

name = confirm_code
pattern = [a-zA-Z0-9]{8}

https://docs.djangoproject.com/en/1.7/topics/http/urls/#named-groups

答案 1 :(得分:0)

听起来可能是你在url配置加载之前渲染包含url标签的模板。

如果在Django加载url配置之前呈现模板,则使用reverse_lazy在这种情况下不起作用。

您可以通过将render_to_string调用移入视图本身来解决问题。