0

One one of my pages, the static url is not rendering correctly. For instance:

<link href="//static/static/css/style.css" rel="stylesheet">

It's a contact form and here's the view:

def contact(request):

    Success = False
    email = ""
    title = ""
    text = ""

    if request.method == "POST":    
        contact_form = ContactForm(request.POST)

        if contact_form.is_valid():
            Success = True
            email = contact_form.cleaned_data['email']
            title = contact_form.cleaned_data['title']
            text = contact_form.cleaned_data['text']

    else:
        contact_form = ContactForm()

    ctx = {'contact_form':contact_form, 'email':email, 'title':title, 'text':text, 'success':success}

    return render_to_response('website/contact.html', ctx, context_instance=RequestContext(request))

Template:

{% if success %}
    <p>{{ email }}</p>
    <p>{{ title }}</p>
    <p>{{ text }}</p>

{% else %}

<form action"." method="POST">

{{ contact_form.as_p }}

<input type="submit" value="Send">
<input type="reset" value="Reset">

</form>
{% endif %}

All other pages are loading corrctly, and the only difference is this form. Does anyone know what's going on?

Edit:

The template code that renders the url is:

<link href="/{{ STATIC_URL }}static/css/bootstrap.css" rel="stylesheet">

And the relevant settings.py code:

STATIC_ROOT = os.path.join(os.path.dirname(__file__), "static")

CURRENT_PATH = os.path.abspath(os.path.dirname(__file__).decode('utf-8')).replace('\\', '/')

STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(CURRENT_PATH, '/static/'),
)
4

1 に答える 1

0

おそらく、重複した「静的」を削除する必要があります。

<link href="{{ STATIC_URL }}/css/bootstrap.css" rel="stylesheet">

編集: 機能していると思われるビューのリクエスト コンテキストを渡していません。リクエスト コンテキストを渡すのを怠ると、STATIC_URL は空の文字列になるため、リンクは正しく解決されます (つまり、/static/css/boostrap.css)。ただし、リクエスト コンテキストを追加すると、STATIC_URL は「/static/」に置き換えられます。間違った URL をレンダリングします。 つまり、これを修正するには: 1) 上記のコードを使用して bootstrap.css を参照し、2) すべてのビューにリクエスト コンテキストを含めます。

詳細については、リクエスト コンテキストのドキュメントを参照してください。

于 2013-03-27T20:59:30.637 に答える