2017-02-21 4 views
0

Ich verwende django, um eine Datenbank zu erstellen, die über eine Website angezeigt werden kann.django und jquery datatables

Bevor ich in mein Projekt einstieg, baute ich die django-App und fügte eine Tabelle in meine Modelle mit nur einer Datenzeile ein. Jetzt versuche ich, diese Tabelle mit jquery-Databases zu visualisieren. Das Format der Jquery-Datenformate wird jedoch nicht angezeigt. Ich habe jede mögliche Lösung ausprobiert, die ich gefunden habe, aber es läuft noch nichts.

Models.py

from django.db import models 

class Variant(models.Model): 
    type = models.CharField(max_length = 20) 
    protein_id = models.CharField(max_length = 20) 
    gene_symbol = models.CharField(max_length = 20) 
    chromosome = models.CharField(max_length = 5) 
    position = models.CharField(max_length = 20) 
    variation = models.CharField(max_length = 20) 
    consequence = models.CharField(max_length = 40) 
    rs = models.CharField(max_length = 20) 
    maf = models.CharField(max_length = 20) 
    phenotype = models.CharField(max_length = 20) 
    source = models.CharField(max_length = 40) 

Views.py

from django.shortcuts import render 
from view_data.models import * 

def variants(request): 
    queryset = Variant.objects.all() 
    return render(request, "variants.html", {"allvars": queryset}) 

Urls.py

from django.conf.urls import url 
from django.contrib import admin 
from view_data import views 
from django.conf import settings 
from django.conf.urls.static import static 

urlpatterns = [ 
    url(r'^admin/', admin.site.urls), 
    url(r'^variants/', views.variants), 
] 

if settings.DEBUG: 
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) 

variants.html

{% load static %} 

<html> 
<head> 
    <script src="{{ STATIC_URL }} js/jquery.js"></script> 
    <script src="{{ STATIC_URL }} jquery.min.js"></script> 
    <script src="{{ STATIC_URL }} jquery.dataTables.min.js"></script> 
    <link rel="stylesheet" href="{{ STATIC_URL }} jquery.dataTables.min.css"> 
    <script> 
     $(document).ready(function(){ 
      $("table_id").dataTable(); 
      }); 
    </script> 
</head> 

<body> 

<div style="width:90%; margin:0 auto;">  
<table id="table_id" class="display" cellspacing="0" width="100%"> 
<thead> 
<tr> 
    <th>Variant Type</th> 
    <th>Protein Id</th> 
    <th>Gene Symbol</th> 
    <th>Chromosome</th> 
    <th>Position</th> 
    <th>Variation</th> 
    <th>Consequence</th> 
    <th>RS Id</th> 
    <th>GMAF</th> 
    <th>Phenotype</th> 
    <th>Source</th> 
</tr> 
</thead> 

<tbody> 
{% for v in allvars %} 
<tr> 
    <td>{{ v.type }}</td> 
    <td>{{ v.protein_id }}</td> 
    <td>{{ v.gene_symbol }}</td> 
    <td>{{ v.chromosome }}</td> 
    <td>{{ v.position }}</td> 
    <td>{{ v.variation }}</td> 
    <td>{{ v.consequence }}</td> 
    <td>{{ v.rs }}</td> 
    <td>{{ v.maf }}</td> 
    <td>{{ v.phenotype }}</td> 
    <td>{{ v.source }}</td> 
</tr> 
{% endfor %} 
</tbody> 
</table> 

</body> 
</html> 

Der statische Ordner, der die Dateien js und css enthält, befindet sich auf derselben Ebene wie meine Django-App (view_data). In der Datei settings.py habe ich 'view_data' und 'jquery' zu INSTALLED_APPS hinzugefügt. Ich gebe auch meine statischen Dateien wie:

STATIC_URL = '/static/' 
STATIC_ROOT = (os.path.join(BASE_DIR, 'static')) 
STATIC_DIRS = (os.path.join(BASE_DIR, 'static')) 

Nun, wenn ich den Server laufen, ich im Terminal die folgenden Zeilen bekam:

[21/Feb/2017 15:33:54] "GET /variants/ HTTP/1.1" 200 1079 
[21/Feb/2017 15:33:54] "GET /variants/jquery.dataTables.min.js HTTP/1.1" 200 1079 
[21/Feb/2017 15:33:54] "GET /variants/jquery.min.js HTTP/1.1" 200 1079 
[21/Feb/2017 15:33:54] "GET /variants/jquery.dataTables.min.css HTTP/1.1" 200 1079 
[21/Feb/2017 15:33:54] "GET /variants/js/jquery.js HTTP/1.1" 200 1079 

Aber wenn ich Website-Link zu überprüfen, ist es zeigt mir die Daten in Zeilen angeordnet, aber ohne Tabellenformatierung. Bitte helfen Sie mir, herauszufinden, was ich falsch gemacht habe und wie ich dieses Problem beheben kann.

Vielen Dank im Voraus. EMH

+0

Möglicherweise möchten Sie auch das gerenderte Markup schreiben. – khalid13

+0

Es tut mir leid, aber ich habe es nicht verstanden. Können Sie mir bitte erklären, was Sie meinen? Vielen Dank. – EMH

Antwort

0

Sie scheinen die # in $("table_id").dataTable();

Es sollte wie sein Vergessenes zu haben:

$("#table_id").dataTable();<br> 
+0

Ich habe es versucht und es zeigt immer noch nicht, danke für die Korrektur. – EMH