Django,从视图中获取不同页面中的数据

时间:2019-03-05 15:32:32

标签: django python-3.x django-2.0

你好,我是Django的新手,我目前正在从事一个项目,但是我不知道该怎么做。

现在我在页面上

home / stats / ID / LANGUAGE

但是在此页面内,我需要一个“编辑”按钮,单击后,我想转到: home / stats / ID / LANGUAGE / edit /

我只想再次从
home / stats / ID / LANGUAGE 中获取相同的数据,但现在从 home / stats / ID / LANGUAGE /编辑/

我的view.py:

class StatsView(TemplateView):
    template_name = 'home/stats.html'
    analytics = build('analyticsreporting', 'v4', credentials=credentials)

    def get(self, request, id, language):

        min_date = "2018-12-01"
        date01 = datetime.strptime(min_date, '%Y-%m-%d')
        max_date = "2018-12-31"
        date02 = datetime.strptime(max_date, '%Y-%m-%d')

        print(date01)
        print(date02)

        if request.GET.get('date1'):

            date1 = request.GET.get('date1')
            pol1 = datetime.strptime(date1, '%Y-%m-%d')
            date01 = pol1
            print(date01)

            if request.GET.get('date2'):

                date2 = request.GET.get('date2')
                pol2 = datetime.strptime(date2, '%Y-%m-%d')
                date02 = pol2
                print(date02)

        if request.user.is_authenticated:
            current_user = request.user.id

            result = BlablaAuth.objects.filter(user=request.user)

            if language == 'ALL':

                blabla = Blabla.objects.filter(blabla=id)

                prefix = '/blabla/' + id

                if result and blabla.count() > 0:

                    analytics_result1 = self.analytics.reports().batchGet(
                        body={
    "Google analytics reporting stuff"
                    analytics_result2 = self.analytics.reports().batchGet(
                        body={
    "Google Reporting stuff"

                    return render(request, self.template_name, context={
                        "report1": analytics_result1.execute(),
                        "report2": analytics_result2.execute()
                    })

            else:

                apple = Apple.objects.filter(video=id)

                prefix = '/apple/' + id

                if result and apple.count() > 0:

                    analytics_result1 = self.analytics.reports().batchGet(
                        body={
    "Google reporting analytics stuff"

                    analytics_result2 = self.analytics.reports().batchGet(
                        body={
    "Google reporting analytics stuff"

                    return render(request, self.template_name, context={
                        "report1": analytics_result1.execute(),
                        "report2": analytics_result2.execute()
                    })

我的urls.py:

from django.conf.urls import url
from django.contrib import admin
from home.views.views import HomeView, StatsView
from .views import views
from django.contrib.auth.decorators import login_required


app_name = "home"
urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^stats/(?P<id>[0-9]+)/(?P<language>[a-zA-Z]+)/$',
        login_required(StatsView.as_view()), name='stats'),
    url(r'^stats/(?P<id>[0-9]+)/(?P<language>[a-zA-Z]+)/edit/$',
        StatsView.edit_stats, name='stats_edit'),
    url(r'^$', login_required(HomeView.as_view()), name='home'),
]

我在stats.html中的按钮:

<button><a href="{% url home:stats_edit auth.blabla.id apple.language %}">Edit</button>

2 个答案:

答案 0 :(得分:2)

假设您的编辑视图将基于通用CBV(例如UpdateView),则可以创建一个具有方法Mixin的{​​{1}}类,并执行您现在所做的所有工作在您的get_context_data(self, **kwargs)的{​​{1}}方法中。 get()TemplateView会自动调用此方法,并将上下文添加到渲染中。

TemplateView

然后在您的视图中:

UpdateView

,并显示您的修改视图:

class AnalyticsMixin(object):
    analytics = None  # or some default that can be used by all subclasses. 
    #  None is not a good default since it will raise an AttributeError when calling self.analytics.reports()

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        # all the stuff you do in get() method, using self.request and self.kwargs which are set by the `as_view()` method on the CBV
        request = self.request
        id = self.kwargs.get('id')
        language = self.kwargs.get('language')
        ...
        return context.update({
            "report1": analytics_result1.execute(),
            "report2": analytics_result2.execute()
        })

答案 1 :(得分:0)

在两个视图之间传递数据的方式是session。 django支持身份验证以及匿名会话。只需将数据存储为会话密钥,并在需要时进行检索。