Create Django Dynamic Hyper Links and get parameters from them

I was trying to use a template to create dynmic hyperlinks and get parameters from them. Searched online and found two ways to do that according to what types of the url you are creating

type 1: http://localhost:8000/pathogen/detaildemo/28

type 2: http://localhost:8000/pathogen/detaildemo/?id=28

For type 2: if you want to get the value of id, 28 in this case, you  will have to use request.GET.get('id')

in template file you have such code to create dynamic hyperlnks

<tbody>
                {% with table.rows as rows %}
                    {% paginate rows %}
                    {% for row in rows %}
                        <tr>
                        {% for value in row %}
                        <td > <a href={% url pathogenbase.apps.pathogen.views.someDemographics %}?id={{row.isolate }}>{{ value }}</a> </td>
                        {% endfor %}
                       
                        </tr>
                    {% endfor %}
                    {% show_pages %}
                {% endwith %}

</tbody>

 

in url file you have

urlpatterns = patterns('',
   
 .......
    url(r'^detaildemo/$', views.someDemographics, name = 'show_some_demographics'),
.......

)

in view file you have

def someDemographics(request):
    isolateId = request.GET.get('id')
    initial_queryset = Demographic.objects.filter(isolate=isolateId)
    demographicTable = DemographicTable (
        initial_queryset,
        order_by=request.GET.get('sort', 'isolate')
    )
    context = {
        'table':demographicTable,
        'a_title':'Demographic',
        'b_title' : '../demographicsearch/',
       
    }
    return render_to_response(
        'pathogen/detaildemographic.html',
        context,
        context_instance=RequestContext(request),
    )

 

I am working on type 1 now.

No comments:

Post a Comment

Datatable static image not found on the server

When you use ```datatables.min.css``` and ```datatables.min.js``` locally, instead of datatables CDN, you may have encountered that ```sort...