开发者

How can you define a persistent variable in Django

I have a Django project, and what I would like to do is be able to create a reference to a variable defined in urls.py that is kept for the duration of the process.

I've tried an approach using global vars - but for whatever reason, the value of the variable in urls.py is the initial value - even if I change it somewhere else. I've followed the concepts as explained here, but just doesn't work.

However, when I try such a scenario outside of Django, it works as expected.

I think I'm missing a trick (or two) with Django - it's great, I'm new to it, and I think I'm going down the wrong path. Should I be using the caching stuff included with Django to store the variable, or is that even more o开发者_如何学JAVAff track?

Many thanks for any pointers.


I agree with S.Lott's comment, your question is a little vague. I think you're trying to reference some variable defined in urls? You know you can import urls.py into any other python script as long as it's on your PYTHONPATH. Assuming you're talking about a Django view when you say a "process", you could import a URL directly into a view function:

def foo_view(request):
    from bar_app.urls import BAR_URL_PATTERN

Please give us more information on your specific use case, and I'll amend my answer to give you a better response.

Good Luck! I hope you're enjoying your first foray into the wonderful world of Django!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜