2017-05-08 17 views
0

私は現在、url.py、view.py、およびhtmlページを調べようとしていますが、djangoを使用してhtmlページを開く際に問題に直面しています。私のコードは下に記載されている:Django html onclick別のhtmlページを開く

batterycurrent.py ** batterycurrent.htmlがdiagnostic.html内にあるurl.py

from __future__ import absolute_import 
from __future__ import unicode_literals 

from collections import OrderedDict 
from django.conf.urls import url 
from django.core.urlresolvers import reverse_lazy 
from .views import BatteryCurrentView 

sub_urlpatterns['diagnostic'] = [ 
    url(r'^diagnostics$', DiagnosticView.as_view(), name='diagnostic'), 
    url(r'^diagnostics/download', DiagnosticDownloadView.as_view(), name='diagnostic-download'), 
    url(r'^diagnostics/batterycurrent', BatteryCurrentView.as_view(), name='batterycurrent'), 
] 

from __future__ import absolute_import 
from __future__ import unicode_literals 

from django.core.files.storage import default_storage 
from django.core.urlresolvers import reverse_lazy 
from django.http import HttpResponseRedirect 
from django.utils import timezone 
from django.views.generic import FormView, TemplateView 
from sendfile import sendfile 
import os.path 

from .mixin import AjaxTemplateMixin, PermissionRequiredMixin, PageTitleMixin 
from ..forms import DiagnosticsForm 
from ..tasks import dump_diagnostics 
from django.shortcuts import render 

class DiagnosticMixin(PageTitleMixin, PermissionRequiredMixin): 
    permission_required = ['system.view_log_files'] 
    page_title = 'Diagnostics' 
    form_class = DiagnosticsForm 

class BatteryCurrentView(DiagnosticMixin,FormView): 
    template_name = 'system/batterycurrent.html' 

    def batterycurrent(request): 
    return render(request, 'system/batterycurrent.html') 

ビューフォルダの下。

batterycurrent.html(テンプレート(HTMLは

<li><a href="{% url 'system:batterycurrent' %}">Battery Current Vs Timestamp</a></li> 

私はコードを実行し始めたとき)、システムフォルダ内にある、エラーが登場

i) importError batterycurrentView couldn't be imported 
ii)Reverse for 'batterycurrent' with arguments '()' and keyword arguments '{}' 
    not found. 0 pattern(s) tried: [] 

この上で私を導いてください。

答えて

1

これは少し難しいですが、完全なアプリレイアウトを提供していませんが、あなたがビューフォルダを持っているようですファイル "batterycurrent.py"を含んでいます。このファイルには、あなたのビューBatteryCurrentViewが含まれています。

ので、だけでなく、あなたがそのディレクトリに(空の)__init__.pyファイルを持っていることを保証する場合は、あなたurls.pyはそれがであるモジュールからビューをインポートする必要があります。また

from views.batterycurrent import BatteryCurrentView 

、Iあなたはそのファイル内のsub_urlpatternsで何をしているのか分かりません。その辞書はどこにも定義しません。いずれにしても、パターンであり、リストはurlpatternsでなければなりません。

最後に、そのファイルがurls.pyであり、url.pyではなく実際に呼び出されていることを確認してください。

+0

申し訳ありません申し訳ありませんが、私のアプリケーションのファイルはここに入れるには大きすぎます。ありがとう!私は__init__.pyにそれを含めるのを忘れていました。 –

0

問題があるように見えるのは、url.pyファイルのモジュールをインポートすることです。

batterycurrent.py内にBatteryCurrentViewモジュールを作成し、別のフォルダにurl.pyファイルを作成した場合は、モジュール名をファイル名のフォルダ名でインポートする必要があります。

だから、あなたのファイルは、ビューのフォルダ内に、あなたは

from views.batterycurrent import BatteryCurrentView 

を使用する必要がありますされ、場合には、ファイルurl.pyは、ビュー内の同じフォルダに存在するならば、あなたはちょうどこのようにモジュールをインポートする必要があります。

from .batterycurrent import BatteryCurrentView 
+0

ありがとう、それは動作します! –

+0

@kit、それはうまくいった。また、[[質問する、回答を得る、散漫する]](((http://stackoverflow.com/tour)))にアクセスして、できるだけ多くの情報を得てサポートしてください。ありがとう。 –

0

ビューにコンテキストを追加した後にもう一度やり直すか、空のままにしてください。

def batterycurrent(request): 
    return render (request, template, {}) 
+0

私の質問へのあなたの応答のおかげで、私はinit.pyファイルのビューをインポートすることを忘れました。 –

関連する問題