django-views

Django pass object from view to next for processing

会有一股神秘感。 提交于 2020-01-12 05:25:17
问题 If you have 2 views, the first uses modelform that takes inputted information from the user (date of birth, name, phonenumber, etc), and the second uses this information to create a table. How would you pass the created object in the first view to the next view so you can use it in the second view's template I'd appreciate any help you can share 回答1: One approach is to put the object into a session in your first view, which you could then retrieve from the request.session in the second view.

Choose queryset for limit_choices_to based on object fields

社会主义新天地 提交于 2020-01-11 12:05:45
问题 I am trying to limit the choices of a foreign field to those other objects who look like the object self. I've tried this: class Model1(models.Model): entry = models.ForeignKey(Model2, limit_choices_to='get_limit_choices_to') number = IntegerField() def get_limit_choices_to(self): return Model2.objects.filter(expenditure_type=self.expenditure_type) class Model2(models.Model): number = IntegerField() but I get the error _filter_or_exclude() argument after ** must be a mapping, not str I don't

Choose queryset for limit_choices_to based on object fields

谁说胖子不能爱 提交于 2020-01-11 12:04:51
问题 I am trying to limit the choices of a foreign field to those other objects who look like the object self. I've tried this: class Model1(models.Model): entry = models.ForeignKey(Model2, limit_choices_to='get_limit_choices_to') number = IntegerField() def get_limit_choices_to(self): return Model2.objects.filter(expenditure_type=self.expenditure_type) class Model2(models.Model): number = IntegerField() but I get the error _filter_or_exclude() argument after ** must be a mapping, not str I don't

Choose queryset for limit_choices_to based on object fields

坚强是说给别人听的谎言 提交于 2020-01-11 12:04:37
问题 I am trying to limit the choices of a foreign field to those other objects who look like the object self. I've tried this: class Model1(models.Model): entry = models.ForeignKey(Model2, limit_choices_to='get_limit_choices_to') number = IntegerField() def get_limit_choices_to(self): return Model2.objects.filter(expenditure_type=self.expenditure_type) class Model2(models.Model): number = IntegerField() but I get the error _filter_or_exclude() argument after ** must be a mapping, not str I don't

get class name for empty queryset in django

坚强是说给别人听的谎言 提交于 2020-01-10 20:15:10
问题 I have empty queryset of model Student students = Students.objects.all() If the above queryset is empty, then how can i get the model(class name)? How can i get the model name for empty queryset? EDIT: How can i get the app name from the queryset? 回答1: >>> students = Students.objects.all() # The queryset's model class: >>> students.model project.app.models.Student # Name of the model class: >>> students.model.__name__ 'Student' # Import path of the models module: >>> students.model.__module__

Provide extra context to all views

半腔热情 提交于 2020-01-10 14:11:59
问题 I'm putting together a project management website for my team using django. My base template includes a sidebar menu which contains a list of all projects and users, linking to a DetailView for that user or project, respectively. My problem is that I need to provide the User and Project models to every view so that I can render that sidebar. I know how to add extra context; the problem is that I feel like I'm violating DRY by modifying the context at each level. Is it possible to simply

How do I send empty response in Django without templates

喜夏-厌秋 提交于 2020-01-10 11:47:16
问题 I have written a view which responds to ajax requests from browser. It's written like so - @login_required def no_response(request): params = request.has_key("params") if params: # do processing var = RequestContext(request, {vars}) return render_to_response('some_template.html', var) else: #some error # I want to send an empty string so that the # client-side javascript can display some error string. return render_to_response("") #this throws an error without a template. How do i do it? Here

Saving Django ModelForm with a ForeignKey

二次信任 提交于 2020-01-10 10:41:07
问题 This is probably a fairly simple question, but I can't seem to figure it out from the Django Docs. I'm trying to save a two ModelForms at once with one being a ForeignKey of another. I'm not sure how to write the logic in the views to ensure these go together properly. models.py class Address(models.Model): address = models.CharField(max_length=100) city = models.CharField(max_length=50) zipcode = models.PositiveIntegerField() class Store(models.Model): name = models.CharField(max_length=100)

Django Dropdown populate from database

南笙酒味 提交于 2020-01-10 08:31:07
问题 If I pass items to the template through the view, and I want the user to select one of the values that gets submitted to a user's record, I would only have dun a for loop in the template right? What would that look like? In the template: <form method="POST" <select> </select> </form> Model: class UserItem(models.Model): user = models.ForeignKey(User) item = models.ForeignKey(Item) class Item(models.Model): name = models.CharField(max_length = 50) condition = models.CharField(max_length = 50)

Django - ModelForm Create or Update? [duplicate]

南笙酒味 提交于 2020-01-09 09:08:14
问题 This question already has answers here : How to update an object from edit form in Django? (2 answers) Closed 3 years ago . I need to have a form that allows the creation or addition of sessions on a planning Model class Session(models.Model): tutor = models.ForeignKey(User) start_time = models.DateTimeField() end_time = models.DateTimeField() Form class SessionForm(forms.ModelForm): class Meta: model = Session exclude = ['tutor'] View to render the form def editor(request): if request.method