Create a website for mobile and PC with django

I am trying to create a website for mobile and pc browser with django. and I'm trying to find a better structure for views and templates. there is what i tried:

1) use a different url (e.g. http://example.com/mobile/ and http://example.com/ OR http://example.com/?c=mobile ) to distinguish between mobile and PC, and match them with different views that set different templates.

2) in the view, set a different template according to USER_CLIENT

3) use the wrapper layer of the view, the actual view simply returns the data to the shell, the shell sets another template.

Is there a general way to handle this in django? any suggestions and comments?

+3
source share
3 answers

Use the Django "sites" framework for mobile version c http://m.example.com.

+1
source

I would recommend solution 3; using the decorator to check the User Agent client and return to another template in the case of a mobile agent.

Ask the decorator to take two arguments: a regular template and a mobile template.

From your view, return the dict. The decorator can go to the rendering function as a context. There is a decorator called "render_to" that does it very well, google for it.

, , , , cookie, .

+1

best practice: use minidetector to add additional information to the request, then use the built-in django request context to pass it to your such templates.

from django.shortcuts import render_to_response
from django.template import RequestContext

def my_view_on_mobile_and_desktop(request)
    .....
    render_to_response('regular_template.html', 
                       {'my vars to template':vars}, 
                       context_instance=RequestContext(request))

then in your template you can enter things like:

<html>
  <head>
  {% block head %}
    <title>blah</title>
  {% if request.mobile %}
    <link rel="stylesheet" href="{{ MEDIA_URL }}/styles/base-mobile.css">
  {% else %}
    <link rel="stylesheet" href="{{ MEDIA_URL }}/styles/base-desktop.css">
  {% endif %}
  </head>
  <body>
    <div id="navigation">
      {% include "_navigation.html" %}
    </div>
    {% if not request.mobile %}
    <div id="sidebar">
      <p> sidebar content not fit for mobile </p>
    </div>
    {% endif %>
    <div id="content">
      <article>
        {% if not request.mobile %}
        <aside>
          <p> aside content </p>
        </aside>
        {% endif %}
        <p> article content </p>
      </aricle>
    </div>
  </body>
</html>
0
source

Source: https://habr.com/ru/post/1731832/


All Articles