django-views

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

这一生的挚爱 提交于 2019-12-02 03:31:39
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_length=20) timestamp = models.DateTimeField(auto_now=False, auto_now_add=True) objects = PostManager() def _

django model form, restrict choices based on value in ForeignKey model

拈花ヽ惹草 提交于 2019-12-02 02:55:13
问题 I have two models: League and Team . Team has a foreign key link to League . I want to be able to set choices available for the Team based on values stored in League . Specifically: class League(models.Model): number_of_teams = models.IntegerField() class Team(models.Model): league = models.ForeignKey(League) draft_slot = models.IntegerField(choices=[(i+1,i+1) for i in range(?????????)]) I recognize I cannot accurately define my draft_slot.choices in the Team model. So I would expect to set

how to stream file to client in django

会有一股神秘感。 提交于 2019-12-02 02:50:10
I want to know how can I stream data to client using django. The Goal The user submits a form, the form data is passed to a web service which returns a string. The string is tarballed ( tar.gz ) and the tarball is sent back to the user. I don't know what's the way. I searched and I found this , but I just have a string and I don't know if it is the thing I want, I don't know what to use in place of filename = __file__ , because I don't have file - just a string. If I create a new file for each user, this won't be a good way. so please help me. (sorry I'm new in web programming). EDIT: $('

django model form, restrict choices based on value in ForeignKey model

左心房为你撑大大i 提交于 2019-12-02 02:27:17
I have two models: League and Team . Team has a foreign key link to League . I want to be able to set choices available for the Team based on values stored in League . Specifically: class League(models.Model): number_of_teams = models.IntegerField() class Team(models.Model): league = models.ForeignKey(League) draft_slot = models.IntegerField(choices=[(i+1,i+1) for i in range(?????????)]) I recognize I cannot accurately define my draft_slot.choices in the Team model. So I would expect to set up Team like so: class Team(models.Model): league = models.ForeignKey(League) draft_slot = models

Using Django to summarize Report

穿精又带淫゛_ 提交于 2019-12-02 02:18:52
I'm trying to produce a table that will show the total maturity amounts for each financial institution that a plan has. A plan is the term I use for person. So each person can have multiple investments. I'm having trouble creating a table that will do this correctly. Currently I have a report that displays each investment sorted by Maturity Date, i.e. 2011,2012, etc. At the bottom I would like to place this summary table, but my query displays duplicates of each financial institution, due to being able to have multiple investments. My current query: list = Investment.objects.all().filter(plan

Pass request.user to view without altering the url

老子叫甜甜 提交于 2019-12-02 01:36:07
I'm trying to write a view where the current logged in user's information is retrieved. My view is written as below, and it works perfectly fine as long as I pass the user to the view in the URL. def index(request, username, template="index.html"): user = get_object_or_404(User, username__iexact=username) expression_qs = Expression.objects.all().order_by('-created') album_qs = Album.objects.all().filter(head__isnull=False, is_public=True).order_by('-created') user_following = user.relationships.following() following_expressions = positive_filter(expression_qs, user_following, 'user') following

ContentFile not saved in Django model FileField

梦想与她 提交于 2019-12-02 01:18:12
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 TO READ THE DATA: def readGeo(data): f = GeojsonData.objects.all().get(id=data.id).gdata f.open(mode =

View didn't return an HttpResponse object. It returned None instead

岁酱吖の 提交于 2019-12-01 23:28:00
问题 The view below is gives me the error when using the POST method. I'm trying to load the model data into a form, allow the user to edit, and then update the database. When I try to Save the changes I get the above error. def edit(request, row_id): rating = get_object_or_404(Rating, pk=row_id) context = {'form': rating} if request.method == "POST": form = RatingForm(request.POST) if form.is_valid(): form.save() return redirect('home.html') else: return render( request, 'ratings/entry_def.html',

Django templates syntax error

穿精又带淫゛_ 提交于 2019-12-01 18:38:27
Is there any problem with the syntax in the following code, there is a error as Invalid block tag: 'else' {% ifequal chat_profile 1 %} {% extends "chatprofile/chat_profile1.html" %} {% else %} {% extends "chatprofile/chat_profile.html" %} {% endifequal %} The documentation states: If you use {% extends %} in a template, it must be the first template tag in that template. Template inheritance won't work, otherwise. So consider using a design where you can use {% include %} instead. True, you must use extends as the first tag, but you can also pass it a variable instead of a fixed string: {%

Simple example of how to use a class based view and django-filter?

别说谁变了你拦得住时间么 提交于 2019-12-01 16:52:04
问题 The example in the documentation, https://django-filter.readthedocs.org/en/latest/usage.html, is I think for a function based view. I am currently researching how to do this with a class based view. def product_list(request): f = ProductFilter(request.GET, queryset=Product.objects.all()) return render_to_response('my_app/template.html', {'filter': f}) 回答1: A bit more digging and I have managed to answer it. I have used the code from here https://github.com/rasca/django-enhanced-cbv. I added