質問編集履歴
2
mixins追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -99,6 +99,17 @@
|
|
99
99
|
|
100
100
|
return df
|
101
101
|
|
102
|
+
def get_week_calendar(self):
|
103
|
+
calendar_context = super().get_week_calendar()
|
104
|
+
calendar_context['df'] = self.get_week_schedules(
|
105
|
+
calendar_context['week_first'],
|
106
|
+
calendar_context['week_last'],
|
107
|
+
calendar_context['week_days']
|
108
|
+
)
|
109
|
+
return calendar_context
|
110
|
+
|
111
|
+
|
112
|
+
|
102
113
|
```
|
103
114
|
|
104
115
|
```
|
@@ -117,6 +128,7 @@
|
|
117
128
|
context.update(calendar_context)
|
118
129
|
|
119
130
|
return context
|
131
|
+
|
120
132
|
```
|
121
133
|
|
122
134
|
views.pyの書き方的に多分変なんだと思いますが、強引にやりました。
|
1
コメントに対しての追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -14,4 +14,116 @@
|
|
14
14
|
```
|
15
15
|
|
16
16
|
調べたところあまりdjangoでpandasで出力している記事がなく(英語は分かりません)情報が少なく行き詰ってます。
|
17
|
-
よろしくお願いします。
|
17
|
+
よろしくお願いします。
|
18
|
+
|
19
|
+
|
20
|
+
追記
|
21
|
+
|
22
|
+
```
|
23
|
+
response = HttpResponse(content_type='text/csv')
|
24
|
+
response['Content-Disposition'] = 'attachment; filename=a.csv'
|
25
|
+
df.to_csv(path_or_buf=response,sep=';',float_format='%.2f',index=False,decimal=",")
|
26
|
+
```
|
27
|
+
上記試しましたが上手くいきませんでした。
|
28
|
+
|
29
|
+
mixins.pyでcsv出力をしています。(views.pyだと上手く行かなかった)
|
30
|
+
だから上手く行かないのでしょうか?
|
31
|
+
|
32
|
+
```
|
33
|
+
mixins.py
|
34
|
+
|
35
|
+
class CsvMixin(Week_CsvMixin):
|
36
|
+
|
37
|
+
def get_week_schedules(self, start, end, days):
|
38
|
+
|
39
|
+
shop = get_object_or_404(Shops, pk=self.kwargs['shops_pk'])
|
40
|
+
|
41
|
+
user= User.objects.filter(shops__shop=shop)
|
42
|
+
|
43
|
+
b =[]
|
44
|
+
for a in user:
|
45
|
+
b.append(a)
|
46
|
+
lookup = {
|
47
|
+
'{}__range'.format(self.date_field): (start, end),
|
48
|
+
|
49
|
+
}
|
50
|
+
queryset = self.model.objects.filter(**lookup)
|
51
|
+
days = {day: [] for day in days}
|
52
|
+
df = pd.DataFrame(days)
|
53
|
+
|
54
|
+
a=1
|
55
|
+
for schedule in queryset:
|
56
|
+
|
57
|
+
if schedule.user in b:
|
58
|
+
if a == 1:
|
59
|
+
user=schedule.user.last_name+' '+schedule.user.first_name
|
60
|
+
date= schedule.date
|
61
|
+
start_time=schedule.get_start_time_display()
|
62
|
+
end_time = schedule.get_end_time_display()
|
63
|
+
time = start_time+'-'+end_time
|
64
|
+
ddf =pd.DataFrame({date:time},index =[user])
|
65
|
+
df = pd.concat([df,ddf],axis=0)
|
66
|
+
df.fillna(" ", inplace=True)
|
67
|
+
a = 2
|
68
|
+
|
69
|
+
if user != schedule.user.last_name+' '+schedule.user.first_name:
|
70
|
+
user=schedule.user.last_name+' '+schedule.user.first_name
|
71
|
+
date= schedule.date
|
72
|
+
start_time=schedule.get_start_time_display()
|
73
|
+
end_time = schedule.get_end_time_display()
|
74
|
+
time = start_time+'-'+end_time
|
75
|
+
ddf =pd.DataFrame({date:time},index =[user])
|
76
|
+
df = pd.concat([df,ddf],axis=0)
|
77
|
+
df.fillna(" ", inplace=True)
|
78
|
+
|
79
|
+
else:
|
80
|
+
user=schedule.user.last_name+' '+schedule.user.first_name
|
81
|
+
date= schedule.date
|
82
|
+
start_time=schedule.get_start_time_display()
|
83
|
+
end_time = schedule.get_end_time_display()
|
84
|
+
time = start_time+'-'+end_time
|
85
|
+
ddf =pd.DataFrame({date:time},index =[user])
|
86
|
+
df[date]= df[date].astype(str)
|
87
|
+
df.at[user,date] =time
|
88
|
+
df.fillna(" ", inplace=True)
|
89
|
+
# csv保存-------------------
|
90
|
+
df.fillna(" ", inplace=True)
|
91
|
+
today = datetime.datetime.today()
|
92
|
+
day = today.strftime("%Y%m%d")
|
93
|
+
# df.to_csv(day + '_list.csv',encoding= "utf_8_sig")←動作はするが、manage.pyと同じ場所に保存される
|
94
|
+
|
95
|
+
↓エラーは出ないが出力はされない
|
96
|
+
response = HttpResponse(content_type='text/csv')
|
97
|
+
response['Content-Disposition'] = 'attachment; filename=a.csv'
|
98
|
+
df.to_csv(path_or_buf=response,sep=';',float_format='%.2f',index=False,decimal=",")
|
99
|
+
|
100
|
+
return df
|
101
|
+
|
102
|
+
```
|
103
|
+
|
104
|
+
```
|
105
|
+
views.py
|
106
|
+
|
107
|
+
class Shift_csv(mixins.CsvMixin, generic.TemplateView):
|
108
|
+
model = Schedule
|
109
|
+
template_name = 'shift/shift_csv.html'
|
110
|
+
date_field = 'date'
|
111
|
+
def get_context_data(self, **kwargs):
|
112
|
+
context = super().get_context_data(**kwargs)
|
113
|
+
shop = get_object_or_404(Shops, pk=self.kwargs['shops_pk'])
|
114
|
+
context['shops']= User.objects.filter(shops__shop=shop)
|
115
|
+
context['shopnum']=self.kwargs['shops_pk']
|
116
|
+
calendar_context = self.get_week_calendar()
|
117
|
+
context.update(calendar_context)
|
118
|
+
|
119
|
+
return context
|
120
|
+
```
|
121
|
+
|
122
|
+
views.pyの書き方的に多分変なんだと思いますが、強引にやりました。
|
123
|
+
viewsでcsv出力のcodeを書くと上手く行きませんでした。
|
124
|
+
```
|
125
|
+
df =context['df']
|
126
|
+
df.to_csv('a.csv')
|
127
|
+
```
|
128
|
+
みたいに書きましたが上手く動作せず、結果mixinsで書く感じになってます。
|
129
|
+
出力されるならどこに書いてもいいんですが、、。
|