Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango - Linking to web sections with buttons in a home page
    text
    copied!<p>Hi everyone and thanks for reading!</p> <p>I'm a very novel user of Django. I decided to move from my basic web to django for learning how to use a framework web. I did the django tutorial but with my project... the way is not being hassle-free at all.</p> <p>Let me first describe briefly my project so you can get an overview of the whole thing. My basic web consists of a home page and sections where you can change different parameters (such as room temperature) or check sensors data.</p> <p>By now, I have the home page and the temperature control section running (with few things to fix, but correctly rendered). I would like to link every section with a button in the home page. This is my home page template:</p> <pre><code>{% extends 'base.html' %} {% block content %} &lt;h1&gt;Here goes a title&lt;/h1&gt; &lt;p&gt;A welcome message here...&lt;/p&gt; &lt;h2&gt;please select an action&lt;/h2&gt; &lt;div id="column_left" class="column"&gt; {% for action in actions_list_left %} &lt;div id="action_box1{{ forloop.counter }}" class="action_box"&gt; &lt;input type="button" onclick= " HELP HERE!! " id= "{{ action.action_name }}_sel" class="action_button"&gt; &lt;div id="{{ action.action_name }}_desc" class="action_desc"&gt; {{ action.description }} &lt;/div&gt; &lt;/div&gt; {% endfor %} &lt;/div&gt; &lt;!--I ommit the right column, containing more actions...--&gt; {% endblock %} </code></pre> <p>I would like to use my "action" model fields to link to other sections. My idea is to use the action name field as the url of that section (to do it in a generic "model" way). For instance, if my home page is at localhost:8000/ and an action name is "temp", i would like the associated button to link to localhost:8000/temp and render the suitable template.</p> <p>As I think is an urls.py issue, I post here my urls files (without import lines)...</p> <p><strong>urls.py</strong></p> <pre><code>admin.autodiscover() urlpatterns = patterns('', url(r'^admin/', include(admin.site.urls)), url(r'^$', include('principal.urls')), url(r'^temp/', include('temp.urls')), ) </code></pre> <p><strong>principal/urls.py</strong></p> <pre><code>urlpatterns = patterns('', url(r'^$', 'principal.views.home', name='home'), ) </code></pre> <p><strong>temp/urls.py</strong></p> <pre><code>urlpatterns = patterns('', url(r'^$', 'temp.views.index', name='temp_index'), url(r'^(?P&lt;pk&gt;\d+)/$', DetailView.as_view( model=Room, template_name='temp/roomTemp.html')), ) </code></pre> <p>I have tried a few ideas that I've read in other threads...</p> <pre><code>{% url temp:index %} </code></pre> <p>This one threw a NoReverseMatch error... 'temp' is not a registered namespace. Then I googled how to get through this and I changed this line in urls.py:</p> <pre><code>url(r'^temp/', include('temp.urls', namespace='temp')), </code></pre> <p>Also in temp/urls.py I changed the line:</p> <pre><code>url(r'^$', 'temp.views.index', name='temp'), </code></pre> <p>Both pages are still rendered OK, but the button is not working (does nothing)... It must be quite simple, but all the url thing seems a bit tricky to me (by the way the syntax sometimes is like a makefile mordor file :P). I'm using Django 1.3</p> <p>Any help would be really appreciated. Also, any "good django coder" comment or explanation would be very useful, just to not make the same mistake again!</p> <p>Thanks in advance,</p> <p>Juan</p> <p>EDIT: I post the fix in the code here just in case someone find it useful:</p> <pre><code>&lt;form action="{% url action.action_url %}"&gt; &lt;input type="submit" id="{{ action.action_name }}_sel" value="" class="action_button"&gt; &lt;div id="{{ action.action_name }}_desc" class="action_desc"&gt; {{ action.description }} &lt;/div&gt; &lt;/form&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload