django-views

Python: How to solve the issue : 'badly formed hexadecimal UUID string' in Django

旧街凉风 提交于 2019-12-20 03:38:10
问题 I have created 'post' model in which I included 'post_id' as the primary key field. When I am trying to create a post, it is raising an error: 'badly formed hexadecimal UUID string'. I would appreciate helping me in solve this. Here's my code. Models.py: class Post(models.Model): post_id = models.UUIDField(primary_key=True, default='uuid.uuid4', editable=False) user = models.ForeignKey(settings.AUTH_USER_MODEL, default=1) from1 = models.CharField(max_length=20) To = models.CharField(max

ContentFile not saved in Django model FileField

你说的曾经没有我的故事 提交于 2019-12-20 02:54:36
问题 I have a problem when saving Strings as file in my Django models, as whenever I try to get the data back, it gives me a ValueError ("attribute has no file associated"). Here's the details: MODEL: class GeojsonData(models.Model): dname = models.CharField(max_length=200, unique=True) gdata = models.FileField(upload_to='data') def __str__(self): return self.dname CODE THAT SAVES THE DATA: cf = ContentFile(stringToBeSaved) gj = GeojsonDatua(dname = namevar, gdata = cf) gj.save() CODE THAT TRIES

RelatedObjectDoesNOTExist-Edit profile

老子叫甜甜 提交于 2019-12-19 11:33:16
问题 Django==1.10.5. I have a problem RelatedObjectDoesNotExist Views.py: def register(request): if request.method == 'POST': user_form = UserRegistrationForm(request.POST) if user_form.is_valid(): # Create a new user object but avoid saving it yet new_user = user_form.save(commit=False) # Set the chosen password new_user.set_password(user_form.cleaned_data['password']) # Save the User object new_user.save() # Create the user profile profile = Profile.objects.create(user=new_user) return render

Django Master-Detail View Plugins

不打扰是莪最后的温柔 提交于 2019-12-19 10:30:26
问题 Let's say I have 3 django apps, app Country , app Social and app Financial . Country is a 'master navigation' app. It lists all the countries in a 'index' view and shows details for each country on its 'details' view. Each country's details include their Social details (from the social app) and their Financial details (from the financial app). Social and Financial both have a detail view (for each country) Is there an elegant way to 'plug' in those sub-detail views into the master detail view

combine modelformset and inlineformset in django views

吃可爱长大的小学妹 提交于 2019-12-19 04:09:28
问题 i have two models class A(models.Model): name = models.CharField(50) type = models.CharField(50) class B(models.Model): field1 = ForeignKeyField(A) value = IntegerField() I'd like to display a number of forms via a ModelFormSet (from model A) where each one of the forms displays in turn InlineFormSets (from model B) for all objects connected to the object. How can i combine both modelformsets (model A) and inline formsets (mdoel B) on save method in django views? 回答1: I'm not sure this is a

How to chain views in Django?

我是研究僧i 提交于 2019-12-19 03:59:11
问题 I'm implementing James Bennett's excellent django-contact-form but have hit a snag. My contact page not only contains the form, but also additional flat page information. Without rewriting the existing view the contact form uses, I'd like to be able to wrap, or chain, the views. This way I could inject some additional information via the context so that both the form and the flat page data could be rendered within the same template. I've heard it mentioned that this is possible, but I can't

Django and root processes

送分小仙女□ 提交于 2019-12-19 03:16:14
问题 In my Django project I need to be able to check whether a host on the LAN is up using an ICMP ping. I found this SO question which answers how to ping something in Python and this SO question which links to resources explaining how to use the sodoers file. The Setting A Device model stores an IP address for a host on the LAN, and after adding a new Device instance to the DB (via a custom view, not the admin) I envisage checking to see if the device responds to a ping using an AJAX call to an

django count of foreign key model

时光总嘲笑我的痴心妄想 提交于 2019-12-19 02:47:20
问题 Hi i want to display count of answers to my question model my model: class Question(models.Model): text = models.TextField() title = models.CharField(max_length=200) date = models.DateTimeField(default=datetime.datetime.now) author = models.ForeignKey(CustomUser) tags = models.ManyToManyField(Tags) def __str__(self): return self.title class Answer(models.Model): text = models.TextField() date = models.DateTimeField(default=datetime.datetime.now) likes = models.IntegerField(default=0) author =

Accessing Django OneToOneField in templates?

假装没事ソ 提交于 2019-12-18 18:51:18
问题 I have the following scenario. class A(models.Model): a = models.IntegerField() class B(models.Model): c = models.OneToOneField(A) d = models.DecimalField() In my templates, I have a list of objects A. In my template how do I access the attribute "d" from my template? Thanks. 回答1: class B(models.Model): c = models.OneToOneField(A, related_name='b') d = models.DecimalField() {{ a.b.d }} 来源: https://stackoverflow.com/questions/6676134/accessing-django-onetoonefield-in-templates

Django request Post json

心已入冬 提交于 2019-12-18 14:47:55
问题 I try to test a view, I receive a json request from the IPad, the format is: req = {"custom_decks": [ { "deck_name": "deck_test", "updates_last_applied": "1406217357", "created_date": 1406217380, "slide_section_ids": [ 1 ], "deck_id": 1 } ], "custom_decks_to_delete": [] } I checked this in jsonlint and it passed. I post the req via: response = self.client.post('/library/api/6.0/user/'+ uuid + '/store_custom_dec/',content_type='application/json', data=req) The view return "creation_success":