django-views

View an Image Using Django CreateView Before Submitting

这一生的挚爱 提交于 2019-12-13 03:26:43
问题 I have a Django CreateView that works as expected. I allow the user to attach a photo and then after they click Submit, the view captures the image as expected. However, I am trying to figure out if there is a way, using CreateView, to show the image that has been captured. As of right now, it shows the file name, but I am trying to get it to show the image instead. I tried a variation of this SO question, but it is not for CreateView and I can't figure out how to translate it. Render image

Objects and attribute errors on browser, after server runs

牧云@^-^@ 提交于 2019-12-13 03:25:55
问题 I'm new to Django and I'm working on an app that display the objects of a primary key. The server runs fine but I get an error on the browser that says: 'master_courses' objects has no attribute 'masterCourses_slug' The code looks like this: from django.shortcuts import render, redirect from .models import master_courses, course_category, course_series def single_slug(requests, single_slug): categories = [c.course_slug for c in course_category.objects.all()] if single_slug in categories:

How to use Django template as a component?

北城余情 提交于 2019-12-13 03:13:12
问题 I have 5 templates: index.html, detail.html, tag.html, login.html, register.html and a base.html All 5 templates will extend base.html . index.html, detail.html, tags.html have a same <section>...</section> html code section with the same data from backend.I want to add this section to base.html , so that don't need to repeat it 3 times in 3 different templates. But the problem is,the login.html and register.html do need this section. I know if it is React.js or Vue.js it will be very easy to

How to extend the locking/transaction between the views?

蓝咒 提交于 2019-12-13 02:49:31
问题 I have the functionality where I want to extend the transaction between two views. When a user clicks on 'Place Order' then the transaction begins and user is redirected to the merchant site to process the payment. In that case, where the control is not in the view where the transaction was written then How do I keep the locking alive unless the user returns on either of the defined callback URLs and then release it. (And, another story is when user don't even land on callback then is there a

Django updateView saving another instance instead of updating

*爱你&永不变心* 提交于 2019-12-13 02:48:14
问题 im trying to update my model but it just creates another instance and i cant figure out why. i was under the impression that all i needed was: class QuestionUpdate(generic.UpdateView): model = models.Question form_class = QuestionForm and it would take care of it for me but that doesnt seem to be the case. im in django 1.11 and running python 3.6. Any and all help is appreciated. models.py class Question(models.Model): class Meta: ordering = ['-date_updated'] # user = models.ForeignKey(User,

django.db.utils.IntegrityError: FOREIGN KEY constraint failed

删除回忆录丶 提交于 2019-12-13 02:14:50
问题 My models.py class Order(models.Model): user = models.ForeignKey(User, blank=True, null=True, on_delete=models.PROTECT) customer_email = models.EmailField(blank=True, null=True, default=None) customer_name = models.CharField(max_length = 64, blank=True, null=True, default=None) customer_phone = models.CharField(max_length = 48, blank=True, null=True, default=None) customer_address = models.CharField(max_length = 128, blank=True, null=True, default=None) total_price = models.DecimalField(max

How can I change the queryset of one of the fields in the form I'm passing to inlineformset_factory

最后都变了- 提交于 2019-12-13 02:06:37
问题 I'm using django extra views: # views.py from django.forms.models import inlineformset_factory from extra_views import (CreateWithInlinesView, UpdateWithInlinesView, InlineFormSet, ) class LinkInline(InlineFormSet): model = Link form = LinkForm extra = 1 def get_form(self): return LinkForm({}) def get_formset(self): return inlineformset_factory(self.model, self.get_inline_model(), form=LinkForm, **self.get_factory_kwargs()) class TargetCreateView(BaseSingleClient, CreateWithInlinesView):

How to create object from QueryDict in django?

本秂侑毒 提交于 2019-12-13 02:04:28
问题 Let's say I got a QueryDict in my POST request, and I'd like to create a new record in database according to this dict. QueryDict: {u'phone': [u'Phone'], u'email': [u'Email'], u'full_name': [u't54'], u'skype': [u'Skype']} Can I do it in one command? What's the best way to go? Thanks 回答1: As long as you don't have multiple values for the same key, you can do: values = QueryDict.dict() if values: YourModel.objects.create(**values) However, I would strongly suggest using a ModelForm to sanitize

IndexError: tuple index out of range

我的未来我决定 提交于 2019-12-13 01:43:33
问题 I was referring to this page of django documentation to write views. Can someone explain what I did wrong ? And what could be the solution self.object_list = self.get_queryset() File "/vagrant/projects/kodeworms/course/views.py", line 23, in get_queryset self.Course = get_object_or_404(Course, name=self.args[0]) IndexError: tuple index out of range My views.py file # Create your views here. from django.views.generic import ListView, DetailView from django.shortcuts import get_object_or_404

How to implement Post/Redirect/Get in django pagination?

笑着哭i 提交于 2019-12-13 01:15:30
问题 I have a view that filters out results for a posted search form: def profile_advanced_search(request): args = {} if request.method == "POST": form = AdvancedSearchForm(request.POST) qs=[] if form.is_valid(): cd = form.cleaned_data s_country=cd['country'] s_province=cd['province'] s_city = cd['city'] if s_country: qs.append(Q(country__icontains = s_country)) if s_province: qs.append( Q(province__icontains=s_province)) if s_city: qs.append( Q(city__icontains=s_city)) f = None for q in qs: if f