django-views

Django Form If condition in view.py with 2 instance

女生的网名这么多〃 提交于 2020-08-06 05:16:38
问题 TO SAVE DATA that is inputted in form in Django i tried tomake it like this I put this in my model.py class Item(models.Model): CATEGORY = ( ('Gudang Kering', 'Gudang Kering'), ('Gudang Basah','Gudang Basah'), ) name = models.CharField(max_length=200,null= True) stock = models.IntegerField(default='0', blank=False, null=True) category = models.CharField(max_length=200,null= True,choices=CATEGORY) reorderlevel = models.IntegerField(default='0', blank=False, null=True) maxreorderlevel = models

Extract Python dictionary from string

我只是一个虾纸丫 提交于 2020-08-04 09:49:27
问题 I have a string with valid python dictionary inside data = "Some string created {'Foo': u'1002803', 'Bar': 'value'} string continue etc." I need to extract that dict. I tried with regex but for some reason re.search(r"\{(.*?)\}", data) did not work. Is there any better way extract this dict? 回答1: From @AChampion's suggestion. >>> import re >>> import ast >>> x = ast.literal_eval(re.search('({.+})', data).group(0)) >>> x {'Bar': 'value', 'Foo': '1002803'} so the pattern you're looking for is

Extract Python dictionary from string

…衆ロ難τιáo~ 提交于 2020-08-04 09:48:14
问题 I have a string with valid python dictionary inside data = "Some string created {'Foo': u'1002803', 'Bar': 'value'} string continue etc." I need to extract that dict. I tried with regex but for some reason re.search(r"\{(.*?)\}", data) did not work. Is there any better way extract this dict? 回答1: From @AChampion's suggestion. >>> import re >>> import ast >>> x = ast.literal_eval(re.search('({.+})', data).group(0)) >>> x {'Bar': 'value', 'Foo': '1002803'} so the pattern you're looking for is

django convert .values_list('datetimefield') to date

穿精又带淫゛_ 提交于 2020-08-03 05:49:28
问题 I would like to convert a a values_list field with a datetime object to a date object. .values_list('time_finished', flat=True) gives me "2016-03-22T18:52:53.486Z" and what I would like is "2016-03-22" Thank you! 回答1: You can use datetime.datetime.date() method to get datetime.date object: >>> dt = datetime.datetime.now() >>> dt datetime.datetime(2016, 4, 12, 15, 54, 48, 401418) >>> dt.date() datetime.date(2016, 4, 12) Use datetime.datetime.strftime to get string: >>> dt.strftime('%Y-%m-%d')

How can I delete the answers (code in body)?

寵の児 提交于 2020-07-23 06:45:18
问题 I am creating a Q&A website for practice, I created the answer and the question model and linked them together, however I can not access the template that I set for the deletion of the answer model, I created a DeleteView to delete the question. Here is the code: views.py : class Politics_post_details(DeleteView): model = PoliticsPost context_object_name = 'politicsposts' pk_url_kwarg = 'qid' def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) # now you can get

How can I delete the answers (code in body)?

人走茶凉 提交于 2020-07-23 06:43:38
问题 I am creating a Q&A website for practice, I created the answer and the question model and linked them together, however I can not access the template that I set for the deletion of the answer model, I created a DeleteView to delete the question. Here is the code: views.py : class Politics_post_details(DeleteView): model = PoliticsPost context_object_name = 'politicsposts' pk_url_kwarg = 'qid' def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) # now you can get

Django view function not being called on click of submit

不想你离开。 提交于 2020-07-22 09:26:12
问题 I have two view functions in my views.py. The first one renders the index.html Second is created for the form action on index.html (update_db). When I click on submit on the index.html file, it changes url to /update1, but the function call has print('HI') and I cannot see that on console. Neither are any new files created after it runs. Intially I had return render(request, 'index.html', {} ) but I am not sure if that should be returned. Is there some problem with my urls.py? views.py from

class based view sum used for class based form validation (without forms.py)

匆匆过客 提交于 2020-07-22 05:19:55
问题 I have been working on this one for a few days and have re-worked how i'd like to handle this functionality on my fantasy sports website: Objective : limit the number of players allowed on a fantasy owner's roster. Django out-of-the-box User = Owner # models.py class Player(models.Model): player_full = models.CharField(max_length=50) player_owner = models.ForeignKey(User, blank=True, null=True, on_delete=models.SET_NULL) image = models.ImageField(default='default_player.jpg', upload_to=

class based view sum used for class based form validation (without forms.py)

孤人 提交于 2020-07-22 05:19:12
问题 I have been working on this one for a few days and have re-worked how i'd like to handle this functionality on my fantasy sports website: Objective : limit the number of players allowed on a fantasy owner's roster. Django out-of-the-box User = Owner # models.py class Player(models.Model): player_full = models.CharField(max_length=50) player_owner = models.ForeignKey(User, blank=True, null=True, on_delete=models.SET_NULL) image = models.ImageField(default='default_player.jpg', upload_to=

Creating UpdateForm by using ModelForm in Django

青春壹個敷衍的年華 提交于 2020-07-21 03:22:51
问题 I have a Django application and I wanna create an update form with ModelForm but I have to get the fields parameter from user_profile model. So I have created my form and view such as below. forms.py; class UpdateForm(forms.ModelForm): class Meta: model = Data fields = [] def __init__(self, *args, **kwargs): input_choices = kwargs.pop('input_choices') super(UpdateForm, self).__init__(*args,**kwargs) self.fields = input_choices views.py; @login_required(login_url = "user:login") def updateData