I'm currently playing with django to get acquainted with it. I want to build a litte cms by myself. In an apps model i define a string database-field which represents the url, a field for the bodytext etc. Now i wonder what would be the best way to integrate the url and its corresponding content from the set of data.
Do I have to use views.py for sending database-fields with url data to urls.py?
Is it better to process a database query within urls.py to get the urls?
I hope you can follow me...
Thanks a lot
I've read this question several times and I'm still not sure exactly what you're asking.
The tutorial tells you exactly how to resolve addresses to db entries: capture the components of the URL in your urlconf, pass them to the view, and query your models in the view dependent on the parameters passed from the URL. If this doesn't answer your question, you'll need to explain further what's puzzling you.
Related
So I have an app, that people login and it shows them a list of data. We recently made some filters and it would be nice if some of the filters were just on by default, which is simple in the code but I want the url to reflect the GET variables Im using for user sanity.
So, I have this:
#settings.py
ACCOUNT_LOGIN_REDIRECT_URL = "status_dashboard"
and
#urls.py
path(
r"dashboard/?status=in_progress",
DashboardView.as_view(),
name="status_dashboard"),
it would work but it escapes the ? in the url for safety, there must be a way to do this thats dead simple but I can only find things on passing POST variables to the view, which I dont need.
So im trying to use my django installation to create a dashboard a combination of all the data from the 4 other models and views. For our use of django we mainly use it for stats so it's generally just pulling numbers out onto the main index page. Right now I have my index template set up as a redirect_to_template and it goes straight to a template (since everything is still static). Im trying to figure out if im going to have to create another app and pull in all the data to a new view & model for this dashboard page, or if I should create sub-templates if that would work to pull the data.
Thanks again!
I think you're better of with pulling the data by using ajax from your dashboard, it will be a better UX when you have a lot of data to fetch. For that you can use one of the known 3rd party apps for creating REST API or change your existing views to deliver json response as well.
To help anyone that has the question in the future. I ended up importing the apps such as
from status import Alert
context['list'] = Alert.objects.filter(All My Filters Here! (link below))[:8]
Then just for loop list in the template!
https://docs.djangoproject.com/en/dev/ref/models/querysets/
Hope this is helpful to someone else!
I would like to view some user information on over half of my website's views.
This information should contain not only trivial username but also some fields from other tables of my project that are associated with current user.
I would also like to put this information into the template that my current view extends, just to keep it DRY.
I already did some research and coded some templatetags hoping that registering tags would help me achieve this but I have no idea how to get user information when there's no request like in views' functions.
Any tips on how to achieve this will be much appreciated. I just started django yesterday and am still a bit confused by it's philosophy.
You can use a context processor to add data to the template context in a DRY way.
In a nutshell, a context processor is simply a function that accepts a request as its first argument, does some additional processing that you add and augments the context with whatever values you want.
You can query an objects models, add the current datetime...pretty much anything you can do with Python or Django can go into a context processor.
I want to check the useragent using Django inside my template. I know this is possible using JavaScript, but I wanted a server side solution.
I know I can use HttpRequest.META in some middleware class, which I am not currently looking for. I want to determine this using some code in the template itself, without any dependency on other files / classes.
Can anybody help?
You need to use context processors, more specifically django.core.context_processors.request.
This SO answer covers it quiet well:
How can I pass data to any template from any view in Django?
Especially this blog post, that is referenced in the SO answer:
http://www.b-list.org/weblog/2006/jun/14/django-tips-template-context-processors/
I've produced a few Django sites but up until now I have been mapping individual views and URLs in urls.py.
Now I've tried to create a small custom CMS but I'm having trouble with the URLs. I have a database table (SQLite3) which contains code for the pages like a column for header, one for right menu, one for content.... so on, so on. I also have a column for the URL. How do I get Django to call the information in the database table from the URL stored in the column rather than having to code a view and the URL for every page (which obviously defeats the purpose of a CMS)?
If someone can just point me at the right part of the docs or a site which explains this it would help a lot.
Thanks all.
You dont have to to it in the flatpage-way
For models, that should be addressable, I do this:
In urls.py I have a url-mapping like
url(r'(?P<slug>[a-z1-3_]{1,})/$','cms.views.category_view', name="category-view")
in this case the regular expression (?P<slug>[a-z1-3_]{1,}) will return a variable called slug and send it to my view cms.views.category_view. In that view I query like this:
#render_to('category.html')
def category_view(request, slug):
return {'cat':Category.objects.get(slug=slug)}
(Note: I am using the annoying-decorator render_to – it is the same as render_to_response, just shorter)
Edit This should be covered by the tutorial. Here you find the url-configuration and dispatching in every detail. The djangobook also covers it. And check pythons regex module.
Of course you can use this code.
Your question is a little bit twisted, but I think what you're asking for is something similar to how django.contrib.flatpages handles this. Basically it uses middleware to catch the 404 error and then looks to see if any of the flatpages have a URL field that matches.
We did this on one site where all of the URLs were made "search engine friendly". We overrode the save() method, munged the title into this_is_the_title.html (or whatever) and then stored that in a separate table that had a URL => object class/id mapping.ng (this means it is listed before flatpages in the middleware list).