django variable of one view to another from session - django

I m very confused on this and I dont have any idea how to do this..
I have a view where I have listed all the news from my news table. To display the news I have passed context data in list view. Here is my view
class SingleNewsView(ListView):
model = News
form_class = SearchForm
template_name = "single_news.html"
# def post(self, request, **kwargs):
# print "request"
# form = SearchForm(request.user)
def get(self, request, pk, **kwargs):
#form = SearchForm(request.user)
self.pk = pk
self.pub_from = request.GET.get('pub_date_from',False)
self.pub_to = request.GET.get('pub_date_to',False)
self.crawlers = request.GET.get('crawler',False)
print self.crawlers
return super(SingleNewsView,self).get(request,pk, **kwargs)
def get_context_data(self, **kwargs):
context = super(SingleNewsView,self).get_context_data(**kwargs)
context["form"] = SearchForm
if self.pub_from and self.pub_to and self.crawlers:
context["something"] = News.objects.filter(category_id=self.pk).filter(published_date__range=(self.pub_from,self.pub_to), crawler=self.crawlers)
else:
context["something"] = News.objects.filter(category_id=self.pk)
return context
and I have written view that I referenced from django doc to download the news in csv format.. I have also included a search form to filter the news. In my first view I have passed context["something"] to display the list of news in the template.
Now what I want is to download that news in csv. I have written a view for this
def CSVView(request):
response = HttpResponse(content_type="text/csv")
response["Content-Disposition"] = 'attachment; filename=somefilename.csv"'
some_val = request.session["something"]
print some_val
print "this"
writer = csv.writer(response)
writer.writerow(some_val)
return response
This is my next view to download the csv. Here what I am trying to do is to download the news that come after filter. In my first view context["something "] gives the list of news. I did all but dont know how to get it. Lastly I m trying to get the value of contxt["something"] from session but I am failed in that too. How can I get the value of one view to another. Or anyone have better idea how can I download the news that is returned by context["something"]. What am I doing wrong.

Setting data in context does not put it in session. You need to set data in session to store it there. Also, storing objects directly in session is not a good idea. You may have to serialize them.
Better way would be create list of pks of objects you want into session.
Something like:
def get_context_data(self, **kwargs):
context = super(SingleNewsView,self).get_context_data(**kwargs)
context["form"] = SearchForm
if self.pub_from and self.pub_to and self.crawlers:
qs = News.objects.filter(category_id=self.pk).filter(published_date__range=(self.pub_from,self.pub_to), crawler=self.crawlers)
else:
qs = News.objects.filter(category_id=self.pk)
context["something"] = qs
#set data in session
self.request.session['something_pks'] = [ i.pk for i in qs ]
return context
Then in CSVView you can get them with `request.session['something_pks'] and do the query for objects.

Related

Passing Form Querystring (custom queryset) to Django Detailview

I have a form:
class MyTestForm(forms.Form):
options = (
("1", "deck1"),
("2", "deck2"),
("3", "deck3"),
("4", "deck4"),
)
choices = forms.MultipleChoiceField(choices = options, widget=forms.CheckboxSelectMultiple, required = False)
and a views.py which passes the querystring of the form:
class NewFormView(LoginRequiredMixin, View):
def get(self, request, *args, **kwargs):
form = MyNewForm()
context = {'form':form }
return render(request, 'testformv2.html', context)
def post(self, request, *args, **kwargs):
form = MyNewForm(data=request.POST)
if form.is_valid():
data=form.cleaned_data
base_url = reverse('newcombined', kwargs={'pk':24}) ## change this
querystring = '{}?'.format(base_url)
myqueries = [query for query in data if data[query] != []] # take the non-empty selections of the form
for query in myqueries:
if len(data[query]) == 1:
query_arguement = str(query)+'='+str(data[query][0]) +'&'
querystring += query_arguement
elif len(data[query])>1:
query_argument = str(query)+'='+','.join(data[query])+'&'
querystring += query_argument
else:
print('error occured for some reason')
url = querystring[:-1] #takes the last character & sign off
return redirect(url)
return render(request, 'testformv2.html', {'form':form})
so i will end up with a url that looks like "/v2/test/25/?Decks=2,4" ... my problem is I want to use the querystring filters to filter for a specific queryset, however, i must provide DetailView with a primary key in the URL. I am wondering if i should be processing the queryset in the post request of the form (when the form is submitted), OR if i should get the primary key of one of the objects in the queryset, and THEN get the custom filtered queryset in the DetailView. (my problem is more complicated than this because i have a form on the detailview which uses get_object() function, so i cannot use ListView.

Django and Bokeh: How do I add my graph to a Class Detail View?

I can't seem to figure out how to add my graph to a Class Detail View? Is it not possible to do so? I add it to the detailView, and call it in my template with:
{{ div | safe }}
But it does not show? I've gotten it to work perfectly fine in a view and template separately.
Here's the whole detailview I'm trying to implement it into.
DetailView
class MedarbejderDetailView(FormMixin, DetailView):
template_name = 'evalsys/medarbejder/detail.html'
model = Medarbejder
form_class = OpretEvalForm
def evalgraph(self):
colors = ["#40e862", "#ff9d26", "#ff1424"]
over = 0
møder = 0
under = 0
none = 0
counts = []
items = ["Overstiger forventning", "Møder forventning", "Under forventning", "Ingen bedømmelse"]
eval_vudering = Evaluering.objects.values("vuderingsnavn__vuderingsnavn")
source = ColumnDataSource(data=dict(items=items, counts=counts))
for i in eval_vudering:
if "Overstiger forventning" in i.values():
over += 1
elif "Møder forventning" in i.values():
møder += 1
elif "Under forventning" in i.values():
under += 1
elif None in i.values():
none += 1
counts.extend([over, møder, under, none])
plot = figure(x_range=items, plot_height=500, plot_width=500, title="Opsumering af evalueringer",
toolbar_location=None, tools="pan, wheel_zoom, box_zoom, reset, tap", tooltips="#items: #counts")
plot.title.text_font_size = "20pt"
plot.vbar(x="items", top="counts", width=0.9, source=source, legend="items", line_color='black',
fill_color=factor_cmap("items", palette=colors, factors=items))
plot.legend.label_text_font_size = "13pt"
script, div = components(plot)
return render(self, 'evalsys/medarbejder/detail.html', {'script': script, 'div': div})
def view_medarbejder_with_pk(self, pk=None):
if pk:
medarbejder = Medarbejder.objects.get(pk=pk)
else:
medarbejder = self.medarbejder
args = {'medarbejder': medarbejder}
return render(self, 'evalsys/medarbejder/detail.html', args)
def get_context_data(self, **kwargs):
context = super(MedarbejderDetailView, self).get_context_data(**kwargs)
context['eval_list'] = Evaluering.objects.all()
context['fag_list'] = Fag.objects.all()
context['ma'] = Medarbejder.objects.get(pk=self.kwargs.get('pk'))
context['instruktør'] = User.objects.get(username=self.request.user)
return context
def post(self, request, pk):
self.object = self.get_object()
form = self.get_form()
if form.is_valid():
print(form.cleaned_data)
instance = form.save(commit=False)
instance.instruktør = request.user
instance.ma = self.object
return self.form_valid(form)
else:
return self.form_invalid(form)
def form_valid(self, form):
item = form.save()
self.pk = item.pk
return super(MedarbejderDetailView, self).form_valid(form)
def form_invalid(self, form):
return super(MedarbejderDetailView, self).form_invalid(form)
def get_success_url(self):
return reverse_lazy("evalsys:view_evaluering_with_pk", kwargs={'pk': self.pk})
URLs
path('se_alle_evalueringer/<int:pk>', views.MedarbejderEvalDetailView.as_view(), name="view_evaluering_with_fag"),
I know I'm calling the function "view_evaluering_with_fag", so it is because I'm not calling my Bokeh function "evalgraph"?
Didn't know folks couldn't use this as a way to ask follow up questions when we're researching the same question.
Anyhow, I am so about to make your day! With A LOT of trial and error (I've only been coding with Python and Django for a month with no real coding background except Java back in 1999) I got bokeh to render from detailview. It appears the trick is to get stuff under the get_context_data function. I don't know how I came to this conclusion, but I figured the script and div context weren't making their way to the render so I was trying to get them into context. As you'll see below, I put script and div as context['script']=script and context['div']=div. My situation looks a bit simpler, I'm just parsing a bike data file and plotting the data, but hopefully this gets you on your way if you're still trying to make this work.
class FitFileDetailView(DetailView):
model = FitFiles
def get_context_data(self, **kwargs):
model = FitFiles
ff = FitFiles.objects.get(pk=self.kwargs.get('pk'))
ffile = ff.fitfiles.path
fitfile2 = FitFile(ffile)
while True:
try:
fitfile2.messages
break
except KeyError:
continue
workout2 = []
for record in fitfile2.get_messages('record'):
r2 = {}
# Go through all the data entries in this record
for record_data in record:
r2[record_data.name] = record_data.value
workout2.append(r2)
df2 = pd.DataFrame(workout2)
df2['time']=(df2['timestamp'] - df2['timestamp'].iloc[0]).astype('timedelta64[s]')
#Bokeh code
df2 = pd.DataFrame(workout2)
df2['time']=(df2['timestamp'] - df2['timestamp'].iloc[0]).astype('timedelta64[s]')
p2 = figure(x_axis_label='time', y_axis_label='watts', tools="", plot_width=1000, plot_height=500)
p2.line(df2['time'], df2['power'])
p2.line(df2['time'], df2['heart_rate'], color='red')
script, div = components(p2)
context = super(FitFileDetailView, self).get_context_data(**kwargs)
context['script']=script
context['div']=div
return context

Django CBV return different page based on form data

Using CreateView, I am trying to save and return a url based on the items selected in the form. This is for a workout log site where the second form you are directed to is based on the type of workout you want, based on info entered in the first form. I would like to do this without Javascript:
class WorkoutLogCreateView(CreateView):
model = Workout_Log
template_name = 'workout/workout_log_create.html'
fields = ['date_time', 'modality', 'workout_type', 'workout_model']
#something like this:
def get_success_url(self):
if workout_type = '5 rounds'
return reverse('five-rounds-log-create')
elif workout_type = 'Drop Reps'
return reverse('drop-reps-log-create')
else
return reverse('workout-log-menu')
def get_context_data(self, **kwargs):
context = super(WorkoutLogCreateView, self).get_context_data(**kwargs)
context['action'] = reverse('workout-log-create')
return context
In the get_success_url method, you can access the object with self.object.
def get_success_url(self):
if self.object.workout_type = '5 rounds'
return reverse('five-rounds-log-create')
elif self.object.workout_type = 'Drop Reps'
return reverse('drop-reps-log-create')
else
return reverse('workout-log-menu')

Django - create a new object based on another object by prefilling the form and corresponding inline formsets

I want to offer users the possibility to create a new publication based on an existing publication. To do that, I want them to click a link to "basedview" that contains the id of the publication they want the new item to base on. There are two formsets for n:n relations included.
that should open a prefilled form with all fields prefield with the data from the publication it's based on. once the user has made changes as needed, it should then save a new publication and new relations for the fieldset - the latter being the difficult part of it.
So my question is - how can I load all corresponding formsets from the database and then delete all their pk but still keep the relation to the publication item?
Right now it is like this in the get method:
self.object = None
try:
self.object = KombiPublikation.objects.get(pk=self.kwargs['pk'])
except ObjectDoesNotExist:
raise Http404("Keinen Output unter dieser PubID gefunden.")
form = KombiPublikationForm(instance=self.object)
pubspr_formset = KombiPublikationSpracheFormset(instance=self.object)
pubpers_formset = KombiPublikationPersonFormset(instance=self.object)
But that ends up to be just an edit of the existing publication. I somehow have to delete the pk after I populated the formset or find a way to populate the formset differently. Any Ideas?
Thank you very much!
Here the full code excerpt:
class PublikationBasedView(PublikationCreateView):
def get(self, request, *args, **kwargs):
self.object = None
try:
self.object = KombiPublikation.objects.get(pk=self.kwargs['pk'])
except ObjectDoesNotExist:
raise Http404("Keinen Output unter dieser PubID gefunden.")
#todo: delete the pk of all objects in forms in formset, else they stay the same and are also changed!!
#fix: delete pk in objekt in order to save it as a new objekt - else based does not work at all!
#self.object.pk=None
form = KombiPublikationForm(instance=self.object)
pubspr_formset = KombiPublikationSpracheFormset(instance=self.object)
pubpers_formset = KombiPublikationPersonFormset(instance=self.object)
return self.render_to_response(
self.get_context_data(
form=form,
pubspr_formset=pubspr_formset,
pubpers_formset=pubpers_formset,
)
)
#its based on this create view
class PublikationCreateView(LoginRequiredMixin, ShowNumberOfItems, CreateView):
form_class = KombiPublikationForm
template_name = 'output/pub_create.html'
model = KombiPublikation
def get(self, request, *args, **kwargs):
self.object = None
form_class = self.get_form_class()
form = self.get_form(form_class)
pubspr_formset = KombiPublikationSpracheFormset()
pubpers_formset = KombiPublikationPersonFormset()
return self.render_to_response(
self.get_context_data(
form=form,
pubspr_formset=pubspr_formset,
pubpers_formset=pubpers_formset
)
)
def post(self, request, *args, **kwargs):
self.object = None
form_class = self.get_form_class()
form = self.get_form(form_class)
pubspr_formset = KombiPublikationSpracheFormset(self.request.POST)
pubpers_formset = KombiPublikationPersonFormset(self.request.POST)
if form.is_valid() and pubspr_formset.is_valid() and pubpers_formset.is_valid():
return self.form_valid(form, pubspr_formset, pubpers_formset)
else:
return self.form_invalid(form, pubspr_formset, pubpers_formset)
def get_success_msg(self):
return 'Ihr Output wurde erfolgreich unter PubID {} angelegt. Speicherort: {}. <br>'.format(self.object.pk, self.object.status)
def form_valid(self, form, pubspr_formset, pubpers_formset):
""" Called if all forms are valid."""
self.object = form.save()
pubspr_formset.instance = self.object
pubspr_formset.save()
pubpers_formset.instance = self.object
pubpers_formset.save()
messages.success(self.request, self.get_success_msg())
return redirect(self.get_success_url())
def form_invalid(self, form, pubspr_formset, pubpers_formset):
""" Called if whether a form is invalid. Re-renders data-filled forms and errors."""
return self.render_to_response(
self.get_context_data(
form=form,
pubspr_formset=pubspr_formset,
pubpers_formset=pubpers_formset,
))
Once you've set the Form's instance it's bound to that object. All updates will be to the object you passed.
Instead you need to set the Form's initial value
Use initial to declare the initial value of form fields at runtime. For example, you might want to fill in a username field with the username of the current session.
Then there's a utility at django.forms.models.model_to_dict that'll give you the dict you need for initial:
Returns a dict containing the data in instance suitable for passing as a Form's initial keyword argument.
So you'll need to do something like this:
from django.forms.models import model_to_dict
object = # Your code here...
# You don't want `id`. Possibly others...?
initial_data = model_to_dict(object, exclude=['id'])
form = YourFormClass(initial=initial_data)
# ...
Hopefully that helps.
I solved the problem and since it was a bit more complicated then expected, I share my finding here - if someone finds a simpler solution feel free to add another comment
That is the final get method in the view:
def get(self, request, *args, **kwargs):
self.object = None
try:
self.object = KombiPublikation.objects.get(pk=self.kwargs['pk'])
except ObjectDoesNotExist:
raise Http404("Keinen Output unter dieser PubID gefunden.")
#load all form initials and render the form correctly - but save new objects
#1. make sure the main publikation object is saved as a new object:
self.object.pk = None
self.object.erstellungsdatum = datetime.now()
form = KombiPublikationForm(instance=self.object)
#2. get the corresponding querysets for sprache and person:
pubspr = KombiPublikationSprache.objects.filter(publikation=self.kwargs['pk'])
pubpers = KombiPublikationPerson.objects.filter(publikation=self.kwargs['pk'])
#make a list of dicts out of the querysets and delete pk id and fk relations
pubspr_listofdicts = []
for pubspr in pubspr:
pubspr_dict= model_to_dict(pubspr)
del pubspr_dict['id']
del pubspr_dict['publikation']
pubspr_listofdicts.append(pubspr_dict)
pubpers_listofdicts = []
for pubpers in pubpers:
pubpers_dict=model_to_dict(pubpers)
del pubpers_dict['id']
del pubpers_dict['publikation']
pubpers_listofdicts.append(pubpers_dict)
#create new formsets with the right amount of forms (leng(obj_listofdicts)
KombiPublikationSpracheFormset = inlineformset_factory(KombiPublikation,
KombiPublikationSprache,
form=KombiPublikationSpracheForm,
extra=len(pubspr_listofdicts),
can_delete=True,
can_order=True,
min_num=1,
validate_min=True)
KombiPublikationPersonFormset = inlineformset_factory(
KombiPublikation,
KombiPublikationPerson,
form=KombiPublikationPersonForm,
extra=len(pubpers_listofdicts),
can_delete=True,
can_order=True,
min_num=0,
validate_min=True)
#initiate the formset with initial data:
pubspr_formset = KombiPublikationSpracheFormset(instance=self.object, initial=pubspr_listofdicts)
pubpers_formset = KombiPublikationPersonFormset(instance=self.object, initial=pubpers_listofdicts)
return self.render_to_response(
self.get_context_data(
form=form,
pubspr_formset=pubspr_formset,
pubpers_formset=pubpers_formset,
)
)

Django ListView - Form to filter and sort

My Goal
A site that list all my Updates (model) in a table
Dont display all models at once (pagination - maybe 10 per page)
Filter and sort the list
My thoughts
I can use ListView to get a set of all my Updates
Use paginate_by = 10
Use a form to set order_by or filter in my QuerySet
My Problem
I am not sure how to add an form to modify my QuerySet with filter and sortings. My Idea was to modify the Query in get_queryset with additional filter and order_by.
My View
class MyView(ListView):
model = Update
template_name = "updates/update.html"
paginate_by = 10
def get_queryset(self):
return Update.objects.filter(
~Q(state=Update.STATE_REJECTED),
~Q(state=Update.STATE_CANCELED),
~Q(state=Update.STATE_FINISHED),
).order_by(
'planned_release_date'
)
My Idea
Something like this. I know it's not working like this ... just to illustrate
class MyView(ListView):
model = Update
template_name = "updates/update.html"
paginate_by = 10
def post(self, request, *args, **kwargs):
new_context = Update.objects.filter(
request.POST.get("filter"),
).order_by(
request.POST.get("sorting"),
)
def get_queryset(self):
return Update.objects.filter(
~Q(state=Update.STATE_REJECTED),
~Q(state=Update.STATE_CANCELED),
~Q(state=Update.STATE_FINISHED),
).order_by(
'planned_release_date'
)
You don't need post. Pass the filter value and order_by in the url for example:
.../update/list/?filter=filter-val&orderby=order-val
and get the filter and orderby in the get_queryset like:
class MyView(ListView):
model = Update
template_name = "updates/update.html"
paginate_by = 10
def get_queryset(self):
filter_val = self.request.GET.get('filter', 'give-default-value')
order = self.request.GET.get('orderby', 'give-default-value')
new_context = Update.objects.filter(
state=filter_val,
).order_by(order)
return new_context
def get_context_data(self, **kwargs):
context = super(MyView, self).get_context_data(**kwargs)
context['filter'] = self.request.GET.get('filter', 'give-default-value')
context['orderby'] = self.request.GET.get('orderby', 'give-default-value')
return context
Make sure you give proper default value to filter and orderby
Example form (you can modify this to your need):
<form method="get" action="{% url 'update-list' %}">
<p>Filter: <input type="text" value={{filter}} name="filter"/></p>
<p>order_by: <input type="text" value={{orderby}} name="orderby"/></p>
<p><input type="submit" name="submit" value="submit"/></p>
</form>
I am wondering why nobody mentioned here this cool library: django-filter https://github.com/carltongibson/django-filter
you can define your logic for filtering very clean and get fast working forms etc.
demo here: https://stackoverflow.com/a/46492378/953553
I posted this elsewhere but I think this adds to the selected answer.
I think you would be better off doing this via get_context_data. Manually create your HTML form and use GET to retrieve this data. An example from something I wrote is below. When you submit the form, you can use the get data to pass back via the context data. This example isn't tailored to your request, but it should help other users.
def get_context_data(self, **kwargs):
context = super(Search, self).get_context_data(**kwargs)
filter_set = Gauges.objects.all()
if self.request.GET.get('gauge_id'):
gauge_id = self.request.GET.get('gauge_id')
filter_set = filter_set.filter(gauge_id=gauge_id)
if self.request.GET.get('type'):
type = self.request.GET.get('type')
filter_set = filter_set.filter(type=type)
if self.request.GET.get('location'):
location = self.request.GET.get('location')
filter_set = filter_set.filter(location=location)
if self.request.GET.get('calibrator'):
calibrator = self.request.GET.get('calibrator')
filter_set = filter_set.filter(calibrator=calibrator)
if self.request.GET.get('next_cal_date'):
next_cal_date = self.request.GET.get('next_cal_date')
filter_set = filter_set.filter(next_cal_date__lte=next_cal_date)
context['gauges'] = filter_set
context['title'] = "Gauges "
context['types'] = Gauge_Types.objects.all()
context['locations'] = Locations.objects.all()
context['calibrators'] = Calibrator.objects.all()
# And so on for more models
return context
This is how we do it, that way you get validation/type conversion as well:
class UnitList(PermissionRequiredMixin, ListView):
""" Class based view to show a list of all buildings for a specific user """
model = Unit
ordering = ['building', 'unit']
paginate_by = 100
# Access
permission_required = ['core.manager_perm']
raise_exception = True # If true, give access denied message rather than redirecting to login
def get_queryset(self):
try:
units = self.model.objects.filter(building__company=self.request.user.profile.company)
except Profile.DoesNotExist:
units = self.model.objects.none()
form = UnitSearchForm(self.request.GET)
if form.is_valid():
filters = {}
address = form.cleaned_data['address']
neighborhood = form.cleaned_data['neighborhood']
beds = form.cleaned_data['beds']
amenity = form.cleaned_data['amenity']
if address:
filters['building__street_index__istartswith'] = compute_street_address_index(address)
if neighborhood:
filters['building__neighborhood__icontains'] = neighborhood
if beds:
filters['beds'] = beds
if amenity:
filters['unit_amenities__name__iexact'] = amenity
units = units.filter(**filters)
return units.select_related('building').order_by(*self.ordering)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['form'] = UnitSearchForm(self.request.GET)
return context