Responsive web design with mobile detection interspersed for greater control - python

I have just finished building my first website with a mobile-first responsive design process in mind. The site looks and works great on mobile devices, but the desktop experience is quite lacking. There's no good way I can improve the desktop experience without compromising the mobile experience, and even then there are just some things I can't do using a pure client side approach.
I don't want to have to manage two separate websites (mobile, desktop). What I want to do is have mobile detection server side to have more control of when I load/display more data, etc.
I want to do something akin to:
// in view
...
if not request.mobile:
related_posts = post.get_related_posts()
...
// in template
...
{% if not request.mobile and related_posts %}
{{ related_post_stream(related_posts) }}
{% endif %}
...
Surprisingly, I don't see much online discussion about doing things this way. Most articles I've read either recommend pure (mobile-first) responsive design or a separate mobile website, although this seems like a decent idea. Is there a downside to doing this?

If you are using Django, there is django-mobile module which allows you to check the mobile usage in the template like this:
{% if flavour != "mobile" %}Desktop version{% endif %}
If you are not using Django, you can still analyze the source of that module and implement something similar for your website.

Related

Django built in server keep disconnecting when loading a template

I'm using Django built in server for developing a web application. Upon rendering a template for a specific web page, the server stop working without any log or warning on the console. It's just suddenly disconnected everytime i try to access that page.
After a while, i found out that if i remove a block of code which has the function of creating a dropdown list, the problem disappeared. But it's just a very simple code in order to get a list of departments, iterate it and make a dropdown list. I have seen other people do the same on the internet, so i have no idea why this is happening.
Here is the code of the view:
def edit(request):
departments = Department.objects.filter()
# I can iterate the QuerySet normally
for d in departments:
print(d.id, d.name, d.department_code)
return render(request, 'edit/edit.html', {'departments': departments})
and the block of code which creates a dropdown list:
<select name="department1" class="department">
<option value=""></option>
{% for d in departments %}
<option value="{{ d.id }}"> {{ d.name }} - {{ d.department_code }} </option>
{% endfor %}
</select>
Any help would be appreciated. Thank you.
I fixed the problem. I was using MysqlConnector API to connect to the database. I switch to mysqlclient instead and voilĂ , the server works beautifully now.
What i'm really mad about is i still don't know why that happened. The Django document simply said that you can use MysqlConnector API, there is no warning about any problem at all. The server also kept disconnecting without any trace or log, leaving me in the dark. It took me over 1 day of desperately debugging till i felt like there is something wrong with the connection to the database.
Anyway, thank you all for your helps, hope this can help somebody to avoid this problem as well.
P/S: It took me that long to recognized something wrong with the connection because i can still get the data from the database and print it on the console, so i didn't think there is any problem about that.

Python as a replacement for PHP inside HTML

Is it possible to use Python for web development in the same way that PHP is used?
In other words, can I add some python code among my HTML that gets run on the server and possibly adds to the HTML?
For example, some PHP code:
<p>Cheese:</p>
<ul>
<?php
$cheeses = ['brie', 'cheddar', 'death star'];
foreach ($cheeses as $c){
echo "<li>".$c."</li>";
}
?>
</ul>
<p>No more cheese :(</p>
Instead using python could be:
<p>Cheese:</p>
<ul>
<?py
cheeses = ['brie', 'cheddar', 'death star']
for c in cheeses:
print ("<li>" + c + "</li>")
?>
</ul>
<p>No more cheese :(</p>
Python alone is not a Web Development language. However you may use it with some Web Framework (like Django, Flask, etc.) in order to perform operations in templates using template tags and filters. For Django, refer Django's built-in tags document for the complete list of available operations.
Hence, your equivalent for loop in Django's template will be like:
{% for c in cheeses %}
<li> {{ c }} </li>
{% endfor %}
where you have to pass cheeses list with context object while rendering the template.
As most of the HTTP server are written in C /C++ they do not support python, ruby or php directly. There is special implementation for this specific languages. So your PHP web server will not support python unless python scripting is also implemented. However, you can use CGI (which is still supported) to implement your python scripting. But it is not recommended. Rather you should use existing python web farmeworks like Django, TurboGears, Zope, Bottle or Flasks
Python Web Framework is quite difference from PHP.
There are some template languages like Django's template language, Jinja2template engine. You use tags in HTML template, and even do some computing with django's "widthratio" tag. But it's really not recommended to do this, since template is basically string, python use template with some variables to render a final HTML to response to the browser.
So in python web framework, template is just used to manage the view display, you should separate the logic code with template.
If you want to use python in web development, you better think in python's way, There are many MVC frameworks in python, the best practice is write your python code in controller, leave the view rendering to template.

Smart way of preloading html email template using Django

I'm sending out emails and I'd like to use HTML which is quite lengthy and currently in a file. What do people recommend doing as a way to reload it. The issue with local storage is that it might be costly to read from time wise. On the other hand including in a string/dictionary is possible but this is really messy. What is the recommended approach for storing say 10 HTML templates. I'd like to avoid a DB if I can.
Template caching could be a possible improvement here. You can cache the whole template by using a cached.Loader or different template parts/fragments.
Also, consider using django-debug-toolbar with a template-timings panel to understand where the bottleneck is and where the time is spent:
Template-timings is a panel for Django Debug Toolbar that gives an
in-dept breakdown of the time it takes to render your Django templates
(including templates included via {% extends %} and {% include %}).

Resolving template block structure conflicts with third-party django apps

When incorporating a third-party django app, I typically want it to be integrated aesthetically with the rest of my django project. While this is typically a matter of overriding the apps 'base.html' (if that), we all structure our templates a little differently, so incompatibilities often arise. For instance, suppose an app defines {% block footer %} and and makes use of it for a variety of things throughout its templates. If I am already using {% block footer %} for, say, a nav bar or copyright info, I don't want the app's templates to override my block.
A simpler, related case would be using different block names for the same thing. For instance, {% block extra-head %} versus {% block extrahead %}.
What's the best way of resolving these sorts of situations? Ideally, it would be nice to remap blocks, so you could do things like "put the child's {% block footer %} in the parent's {% block content-footer %}". Is there any way to get close to that? Or is the only solution to simply override every conflicting template?
First, the html inheritance should go:
my-sitebase.html
|-- app-base.html
|-- app-foo-template.html
I think this is what you meant, but the wording was a bit ambiguous. You might be able to just edit the app-base.html.
Second, a reusable app that overrides something like {% block footer %} is almost willfully causing trouble for anyone who uses it -- you should flag that in the provider's issue tracker.
If the app does do anything with {% block footer %} that should be done in the app-base.html area so you only have to change it once when integrating it with your site.
Finally, a recursive find-replace is actually very little effort. If you don't use an IDE that allows this, Text-Crawler is free, lightning fast, and a good non-IDE solution.
There have been a couple of attempts to create a standard inheritance pattern, I put together one that I like for the templates at djangoslingshot.com and I've seen one other -- but so far there hasn't been any coalescence around a standard. Likely because find-replace is actually a very low cost for the benefit of being able to do exactly what you want without someone else's rules getting in your way.

Templating+scripting reverse proxy?

Thinking through an idea, wanted to get feedback/suggestions:
Having had great success with url rewriting and nginx, I'm now thinking of a more capable reverse proxy/router that would do the following:
Map requests to handlers based on regex matching (ala Django)
Certain requests would simply be routed to backend servers - eg. static media, memcached, etc
Other requests would render templates that pull in data from several backend servers
For example, a template could consist of:
<body>
<div>{% remote http://someserver/somepage %}</div>
<div>{% remote http://otherserver/otherpage %}</div>
</body>
The reverse proxy would make the http requests to someserver/somepage and otherserver/otherpage and pull the results into the template.
Questions:
Does the idea make sense or is it a bad idea?
Is there an existing package that implements something like this?
How about an existing server+scripting for implementing this - eg. lighttpd+lua, nginx+??
How about nginx+SSI? Looks pretty capable, if you have experience / recommendations please comment.
How about something like a scripting language+eventlet ?
Twisted?
My preferences are python for scripting and jinja/django style templates, but I'm open to alternatives.
This already exist an is called Deliverance: http://deliverance.openplans.org/
So instead of doing something an AJAXy call into an iframe or something, you're doing it on the server side.
I think it's something I'd only do if the external site was totally under my control, purely for the security implications. It'd also hit your response times quite a bit.
Am I missing the point completely, or would this be quite simple to do with some functions & urllib?

Categories