Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Django - Importing one variable from app1/ models.py to app2/models.py
I'm having difficulties understanding how I can import the "category" variable from Class Questions to my Class post_job. What I want is for me to be possible to add the "category" in the Class post_job. So, when a "job is posted" you can see all the variables including that category. jobs/models.py from django.db import models from django.contrib.auth.models import User # Create your models here. class post_job(models.Model): posizione= models.CharField(max_length=20) descrizione= models.TextField(max_length=60) requisiti= models.TextField(max_length=60) nome_azienda= models.CharField(max_length=20, default=' inserisci nome') email_referente= models.CharField(max_length=20, default='inserisci email') def __str__(self): """String for representing the MyModelName object (in Admin site etc.).""" return self.posizione quiz/models.py from django.contrib.auth.models import User from django.db import models # Create your models here. class Questions(models.Model): CAT_CHOICES = ( ('datascience', 'DataScience'), ('productowner', 'ProductOwner'), ('businessanalyst', 'BusinessAnalyst'), #('sports','Sports'), #('movies','Movies'), #('maths','Maths'), #('generalknowledge','GeneralKnowledge'), ) question = models.CharField(max_length = 250) optiona = models.CharField(max_length = 100) optionb = models.CharField(max_length = 100) optionc = models.CharField(max_length = 100) optiond = models.CharField(max_length = 100) answer = models.CharField(max_length = 100) catagory = models.CharField(max_length=20, choices = CAT_CHOICES) student = models.ManyToManyField(User) class Meta: ordering = ('-catagory',) def __str__(self): return self.question -
I keep getting this error: TypeError: data.map is not a function
i am trying to create a site using django and react, but when i try to view it, i keep getting this error TypeError: data.map is not a function. below is my code, i don't seem to know the problem i am trying to create a site using django and react, but when i try to view it, i keep getting this error TypeError: data.map is not a function. below is my code, i don't seem to know the problem state = { loading: false, error: null, data: [] }; componentDidMount() { this.setState({ loading: true }); axios .get(productListURL) .then(res => { this.setState({ data: res.data, loading: false }); }) .catch(err => { this.setState({ error: err, loading: false }); }); } render() { const { data, error, loading } = this.state; return ( <Container> {error && ( <Message error header="There was some errors with your submission" content={JSON.stringify(error)} /> )} {loading && ( <Segment> <Dimmer active inverted> <Loader inverted>Loading</Loader> </Dimmer> <Image src="/images/wireframe/short-paragraph.png" /> </Segment> )} <Item.Group divided> {data.map(item => { return <Item key={item.id}> <Item.Image src={item.image} /> <Item.Content> <Item.Header as='a'>{item.title}</Item.Header> <Item.Meta> <span className='cinema'>{item.category}</span> </Item.Meta> <Item.Description>{item.description}</Item.Description> </Item.Content> </Item> })} </Item.Group> </Container> ); } } export default ProductList``` -
Donwload MP3 File Django Through Ajax Request
How to download file in django , through ajax call ?. You can assume that , file is ready and you have a file_path) (Note : Don't want to return a file as response, just return a status variable that download is successfull) Please help. -
corseheaders.middleware is not a package
I'm new to django. I'm trying to link my react frontend to my django backend and i need to make the django-cors-headers package work to make axios.post() work. But i keep getting corseheaders.middleware is not a package, although i already installed it using pip and added the config to my settings.py. Any help? This is my settings.py: Django settings for BackEnd project. Generated by 'django-admin startproject' using Django 3.0.5. For more information on this file, see https://docs.djangoproject.com/en/3.0/topics/settings/ For the full list of settings and their values, see https://docs.djangoproject.com/en/3.0/ref/settings/ """ import os # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = '----' # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True ALLOWED_HOSTS = ['localhost','127.0.0.1'] # Application definition INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'corsheaders', 'databaseAPI', ] MIDDLEWARE = [ 'corsheaders.middleware.CorsMiddleware' 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', ] CORS_ORIGIN_ALLOW_ALL=True ROOT_URLCONF = 'BackEnd.urls' TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], }, }, ] WSGI_APPLICATION … -
Django Models Not Fetching different URLS
I am trying to create a music website using python django for backend. I need to fetch different spotify urls for different songs and I recommend it using my Django Admin Panel. The problem I am facing is the moment I define url for second song and the consecutive ones my songs fetch the same url I previously defined for first song. In this way, my every song dialog box plays the same song as the first one defined. Here is the model I defined for My Song: Model Image Here are the changes I made in my HTML file to view the model: Dashboard Image And Finally here is the Views I am using in views.py file of app: Views Image Any help would be highly aprreciated. -
Redirect RabbitMQ new messages to WebClients using Django Channels
I have a map application expected to show entities live location on map. The entities send location to Rabbitmq and I'm supposed yo read these locations and redirect them to web clients. I'm going to use Django channels for implementing websochet part. I set up RabbitMQ as Django Channels Channel_layer. My question is how to read data from RabbitMQ (basic_consume) and send it to web client using websocket. I the websocket connet method I call the consume method to start consuming from rabbitMQ: async def connect(self): await self.accept() await self.channel_layer.group_add("gossip", self.channel_name) obj_rabbit_mq = RabbitMQueue() obj_rabbit_mq.consume("my_channel") and this is the consummer part. I can see the body part in my logs but the part that group_send it doesnt work. class RabbitMQueue: def consume(self, queue): print("Consuming {} ...".format(queue)) self.declare_queue(queue) def callback(ch, method, properties, body): print(" Received {}".format(body)) channels_layer = get_channel_layer() print("channels_layer: {}".format(channels_layer)) # async_to_sync(channels_layer.group_send)( channels_layer.group_send( "gossip", { "type": "user.gossip", "event": "New User", "username": body } ) self.channel.basic_consume( queue=queue, on_message_callback=callback, auto_ack=False) print(' [*] Waiting for messages. To exit press CTRL+C') self.channel.start_consuming() -
Uploading multiple images in one field using django rest framework
I am building a website where people can make a post describing something and uploading multiple images to that post as it is getting created. Also even though I want to be able to upload numerous images at the same time, I want users to be able to delete a single image out of the post. My major concern is how my model or view will be set up in order for users to be able to upload the multiple images as planned. I have tried to do a many to many relationships like in my model below but it just doesn't work class Photo(models.Model): ''' photos model ''' url = models.ImageField(blank=True, null=True) class Post(models.Model): ''' post model, allows users to post pictures and describe their feeling at the moment ''' user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.DO_NOTHING, related_name="post_user") title = models.CharField(max_length=100) description = models.TextField(null=True) photos = models.ManyToManyField(Photo, related_name="assets") likers = models.ManyToManyField(settings.AUTH_USER_MODEL, related_name="post_likers") created_on = models.DateTimeField(auto_now_add=True) updated_on = models.DateTimeField(auto_now=True) def __str__(self): return self.title serializers.py class PhotoSerializer(serializers.ModelSerializer): class Meta: model = Photo fields = ['pk', 'url'] class PostSerializer(serializers.ModelSerializer): user = serializers.SlugRelatedField(read_only=True, slug_field='slug') created_on = serializers.SerializerMethodField(read_only=True) photos = PhotoSerializer() class Meta: model = Post exclude = ['likers', 'updated_on', 'id'] views.py class PostCreateView(generics.CreateAPIView): ''' Create collaborations … -
How to Attach File to LinkedIn by Python code
I am trying to attach pdf file to LinkedIn by Python code. I read some documentation and still didn't find out how to do it. I read some documentation, and they only show to make a post to LinkedIn as text or image. Please help me with this -
How to filter DB in Django using list?
class Student(): name=models.CharField(max_length=200) surname=models.CharField(max_length=200) class group2020(): name=models.CharField(max_length=200) math=models.DecimalField(decimal_places=2,max_digits=1000) english=models.DecimalField(decimal_places=2,max_digits=1000) biology=models.DecimalField(decimal_places=2,max_digits=1000) chemistry=models.DecimalField(decimal_places=2,max_digits=1000) class group2019(): name=models.CharField(max_length=200) math=models.DecimalField(decimal_places=2,max_digits=1000) english=models.DecimalField(decimal_places=2,max_digits=1000) biology=models.DecimalField(decimal_places=2,max_digits=1000) chemistry=models.DecimalField(decimal_places=2,max_digits=1000) class group2018(): name=models.CharField(max_length=200) math=models.DecimalField(decimal_places=2,max_digits=1000) english=models.DecimalField(decimal_places=2,max_digits=1000) biology=models.DecimalField(decimal_places=2,max_digits=1000) chemistry=models.DecimalField(decimal_places=2,max_digits=1000) I combined years in list the following way by subjects: math=[group2020.math,group2019.math,group2018.math] english=[group2020.english,group2019.english,group2018.english] chemistry=[group2020.chemistry,group2019.chemistry,group2018.chemistry] type_1=list(map(lambda n,m:n/m,math,english )) type_2=list(map(lambda n,a:n/a,chemistry,math )) I want to filter from DB students that meets criteria where type_1 >2 and type_2 <1. i know to to make the seperate years using annotaions and filter like using: result = groupA.objects.annotate( type_1=F('math') / F('english'), type_2=F('chemistry') / F('math'), ).filter(type_1__gt=2, type_2__lt=1) However i can get the result of type_1 and type_2 for one year 2020 however i want to get for 3 years in more efficients years rather than doing manually. How to do that. I tried to find in stackoverflow but do not get result for list case inserting in filter. Your help is very much appreciated. -
Django Admin get_queryset with RawQuerySet
I'm developing a product for a client and I've come across the following problem: There's a model I'm working with a RawQuerySet, and I need to show it in the index of this particular application, but by defining get_queryset() inside the ModelAdmin, the application breaks. However if I enter an item directly, it reads it and works correctly. I've been working on this for a day and a half now and I can't think of anything else to do to try and fix it. This is how the get_queryset method looks like: class QuestionnaireAdmin(admin.ModelAdmin): def get_queryset(self, request): return Questionnaire.objects.raw("SELECT * FROM........") And when I enter the admin site of Questionnaires index, this is what I get: Admin Site Error Picture Also, the console doesn't return anything when this happens so I'm a little lost. I'd appreciate any kind of help. Python version is 3.7 and Django version is 2.2 -
django-rest with multiple url parameters
Using djange-rest framework along with Django 3.0 In my urls.py i have these lines: router.register('search/(?P<param_1>\d+)', SomeView, basename='someview') router.register('search/(?P<param_1>\d+)/(?P<param_2>\d+)', SomeView, basename='someview') I have a problem accessing the second url parameter in the second line. It seems like the first line changes the parsing of the second param of the second line. In the SomeView class the self.kwargs dictionary has 'param_1' and 'pk'. If I remove the first line, it all goes well, i get the dictionary populated with 'param_1' and 'param_2'. Is it a weird behavior, or am I missing something? How do I get 'param_2' in the dictionary in this case? -
How to show folders and files under a zip , which was uploaded on django website
I have made a website where users can upload zip files.I want to show files and folders under that zip file and can open the files and edit. How can I do this ? -
TemplateDoesNotExist at /student/login/ , although the template with proper name exits
I am a beginner in Django and I'm trying to learn. I can't figure out the cause of the error, the template name is defined as per the documentation students/student_detail.html. My intuition is that the detail view is not able to find Student Model. or there's not a proper implementation of the DetailView Error TemplateDoesNotExist at /student/login/ /student/5/detail/ Request Method: POST Request URL: http://127.0.0.1:8000/student/login/ Django Version: 3.0.2 Exception Type: TemplateDoesNotExist Exception Value: /student/5/detail/ Exception Location: C:\Users\Madhu\Anaconda3\envs\MyDjangoEnv\lib\site-packages\django\template\loader.py in get_template, line 19 Python Executable: C:\Users\Madhu\Anaconda3\envs\MyDjangoEnv\python.exe Python Version: 3.8.1 Python Path: ['D:\\python\\DJANGO_COURSE_2.xx\\Practice1\\Library', 'C:\\Users\\Madhu\\Anaconda3\\envs\\MyDjangoEnv\\python38.zip', 'C:\\Users\\Madhu\\Anaconda3\\envs\\MyDjangoEnv\\DLLs', 'C:\\Users\\Madhu\\Anaconda3\\envs\\MyDjangoEnv\\lib', 'C:\\Users\\Madhu\\Anaconda3\\envs\\MyDjangoEnv', 'C:\\Users\\Madhu\\Anaconda3\\envs\\MyDjangoEnv\\lib\\site-packages'] Server time: Thu, 30 Apr 2020 06:12:11 +0000 Template-loader postmortem Django tried loading these templates, in this order: Using engine django: This engine did not provide a list of tried templates. urls.py from django.urls import path from . import views #Url patterns app_name = 'students' urlpatterns=[ path('signup/',views.studentsignup, name='signup'), path('<int:pk>/detail/',views.StudentDetailView.as_view(),name='detail'), path('login/',views.loginPage, name='login'), ] views.py def loginPage(request): if request.method=='POST': #if request method is post form = Login(request.POST) print('method is post') if form.is_valid(): # if form is valid then email = form.cleaned_data['email'] password = form.cleaned_data['password'] user = User.objects.get(email=email) print('user is authenticated',user) if user is not None: # if user exists then login(request,user) print('student login done') return render(request,reverse('students:detail',args=[user.student.id])) else: # … -
CheckboxInput always displays ON
I have a CheckboxInput which gets/sets its value to a db, but the CheckboxInput always displays as ON. The db updates correctly and if i am to remove the CheckboxInput and make it a normal checkbox, the output displays correctly. done = forms.NullBooleanField(label='Have you finished', required=False, widget=forms.CheckboxInput(attrs={'checked data-toggle':'toggle', 'data-on':'Yes', 'data-off':'No'})) Thank you -
django.template.exceptions.TemplateSyntaxError: Could not parse the remainder: '+1==2' from '1+1==2'
Django is throwing the following error whenever I open my page : django.template.exceptions.TemplateSyntaxError: Could not parse the remainder: '+1==2' from '1+1==2' Because of this I'm not getting anything in my dropdown list. Please help me solve it. Thank you. html code : {% if 1+1 ==2 %} <form action="{% url 'menu' %}" method="post"> {% csrf_token %} <select name="regplace"> {% for r in regular %} <option>{{r}}</option> {% endfor %} </select> <select name="sicplace"> {% for s in sicilian %} <option>{{s}}</option> {% endfor %} </select> <select name="subplace"> {% for u in subs %} <option>{{u}}</option> {% endfor %}} </select> <select name="salplace"> {% for a in salad %} <option>{{a}}</option> {% endfor %} </select> <select name="toplace"> {% for t in topping %} <option>{{t}}</option> {% endfor %} </select> <select name="paplace"> {% for p in pasta %} <option>{{p}}</option> {% endfor %} </select> <select name="dinplace"> {% for d in dinner %} <option>{{d}}</option> {% endfor %} </select> <input type="submit" value="place order"/> </form> {% endif %} python code : def menu(request): regular = Regular.objects.all() sicilian = Sicilian.objects.all() subs = Subs.objects.all() salad = Salads.objects.all() topping = Toppings.objects.all() pasta = Pasta.objects.all() dinner = Dinner.objects.all() regplac = str(request.POST["regplace"]) sicplac = str(request.POST["sicplace"]) subplac = str(request.POST["subplace"]) salplac = str(request.POST["salplace"]) toplac = str(request.POST["toplace"]) paplac = str(request.POST["paplace"]) dinplac … -
html page missing the alignment in every iteration in Django Template
I have an HTML for cricket fixtures where I need to iterate it through the data in models. If I add a different div tag of the same content in the next line it gets aligned properly but when I'm generating the models data I see the alignment is changed to every iteration. It looks like the image shown here HTML code: <!DOCTYPE html> <html lang="en"> <head> {% load staticfiles %} <meta charset="utf-8"> <link rel="stylesheet" href="{% static 'css/fixture.css' %}"> </head> <body> </header> <div class="page"> <div id="fb-root"></div> <div id="page-wrapper" class="container" style="display:inline-block;"> <div id="shosh" class="ad-unit shosh-embed" style="height:0; width:980px; text-align:center;"></div> <div style="margin-top:5px;"></div> {% for fixture in fixtures %} <div class="cb-bg-white cb-schdl cb-col cb-col-100"> <h1 class='cb-schdl-hdr cb-font-24 line-ht30'>Cricket Schedule</h1> <div id="international-list" class="cb-bg-white"> <div class="cb-col-100 cb-col"> <div class="cb-lv-grn-strip text-bold">{{fixture.date}}</div> <div class="cb-col-100 cb-col"> <div class="cb-col-33 cb-col cb-mtchs-dy text-bold">West Indies tour of England, 2020 (Postponed)</div> <div class="cb-col-67 cb-col"> <div class="cb-ovr-flo cb-col-60 cb-col cb-mtchs-dy-vnu cb-adjst-lst"> <a href="/live-cricket-scores/23239/eng-vs-wi-1st-test-day-1-west-indies-tour-of-england-2020-postponed" title="ENG vs WI, 1st Test, Day 1 Live Cricket Score">England vs West Indies, 1st Test, Day 1</a> <div class="cb-font-12 text-gray cb-ovr-flo">Kennington Oval, London</div> </div> <div class="cb-col-40 cb-col cb-mtchs-dy-tm cb-adjst-lst"> <span class="schedule-date" timestamp="1591264800000" venue="+01:00" format="h:mm a"></span> <div class="cb-font-12 text-gray"> <span>10:00 AM</span> GMT /<span> 11:00 AM</span> LOCAL </div> </div> </div> </div> </div> {% … -
Beautiful Soup - Extract data only contain td tag (without tag like div, id, class....)
I'm new to Beautiful Soup, and I have data like this, which contain 3 set of user data(for this case). I want to get all the information for each USER_ID and save to database. User ID Title Content PID(not every user has this row) Date URL <table align="center" border="0" style="width:550px"> <tbody> <tr> <td colspan="2">USER_ID 11111</td> </tr> <tr> <td colspan="2">string_a</td> </tr> <tr> <td colspan="2"><strong>content: aaa</strong></td> </tr> <tr> <td colspan="2"><strong>date:</strong>2020-05-01 00:00:00 To 2020-05-03 23:59:59</td> </tr> <tr> <td colspan="2"><strong>URL:https://aaa.com</strong></td> </tr> <tr> <td colspan="2">&nbsp;</td> </tr> <tr> <td colspan="2">&nbsp;</td> </tr> <tr> <td colspan="2">USER_ID 22222</td> </tr> <tr> <td colspan="2">string_b</td> </tr> <tr> <td colspan="2"><strong>content: bbb</strong></td> </tr> <tr> <td colspan="2"><strong>date:</strong>2020-05-01 00:00:00 To 2020-05-03 23:59:59</td> </tr> <tr> <td colspan="2"><strong>URL:https://aaa.com</strong></td> </tr> <tr> <td colspan="2">&nbsp;</td> </tr> <tr> <td colspan="2">&nbsp;</td> </tr> <tr> <td colspan="2">USER_ID 33333</td> </tr> <tr> <td colspan="2">string_c</td> </tr> <tr> <td colspan="2"><strong>content: ccc</strong></td> </tr> <tr> <td colspan="2"><strong>date:</strong>2020-05-01 00:00:00 To 2020-05-03 23:59:59</td> </tr> <tr> <td colspan="2"><strong>PID:</strong><strong>ABCDE</strong></td> </tr> <tr> <td colspan="2"><strong>URL:https://ccc.com</strong></td> </tr> <tr> <td colspan="2">&nbsp;</td> </tr> <tr> <td colspan="2">&nbsp;</td> </tr> </tbody> </table> My problem is, All the data are inside td only, and do not contain div name and no parent tag. I can't separate into 3 set of data. I have try the following code, it can find all the USER_ID, but … -
How to change the value of related model
This line of code is iterating through the related queryset: order = Order.objects.get(user=self.request.user, ordered=False) for order_item in order.items.all(): order_item.item.quantity += 1 Its changing the value of quantity (I can see that using (print(order_item.item.quantity)), but it doesn't save in the database. I have tried order.save(), but the value stays the same (before +=1). What I have to do to save new value to the database? -
How do I change a value in django database on button click in HTML?
I have a booleanField in my hostelApplication model, I want to change that field using html button, but I don't know much about js. Please anyone tell me what I am doing wrong. here is my models.py, I have shown only necessary fieds. class hostelApplication(models.Model): hostelName = models.CharField(verbose_name='Select Hostel', max_length=20, choices=hostels, default='shivalik') isApproved = models.BooleanField(default=False) def __str__(self): return self.hostelName I have added my url path in urls as url(r'^ajax/change_status/$', views.ajax_change_status, name='ajax_change_status') here is my html file {% for application in applications %} {% csrf_token %} <div class="table-content"> <div class="table-row"> <div class="table-data">{{application.hostelName}}</div> <div class="table-data">{{application.dateApplied}}</div> <div class="table-data"> <button type="button" class="btn btn-info" id="state">Approve</button> </div> </div> {% endfor %} I have added this js script in this template on the top <script> $("#state").on('click', function () { var username = $(this).val(); var isApproved = true // or false, you have to set it var id = application.id // you have to set it $.ajax({ url: '/ajax/validate_username/', data: { 'csrfmiddlewaretoken': $('input[name="csrfmiddlewaretoken"]').val(), 'isApproved': isApproved 'id': username }, dataType: 'json', success: function (data) { if (data.success) { alert("ajax call success."); // here you update the HTML to change the active to innactive }else{ alert("ajax call not success."); } } }); }); </script> -
Clearing the Session failure: Django Form to pickled ML prediction
I currently have a pickled model for predicting survivability of Titanic based on user inputs of a Django form. After a few days at this, I am now able to finally get a response to the front end of whether someone survived! After submission, i use django.contrib message to send a "survived" or "perished" underneath the form. However, after the first submission, the fields clear in the form, but when i try to enter in new values and submit, i get a '_thread._local' object has no attribute 'value' error. form, error Now, if i restart the django server and refresh the page, i can run the POST again, but just not consecutivetly on the same session. so i dug around and someone had a statement included in a similar project where they "from kera import backend as K" and then added below right before the return in the function: K.clear_session() But when i put that into my code, I get the same Attribute Error as before but i get it on the first form submission, rendering it copletely useless. So, how can I use the form to repeatedly query the model with each use being independent. the outcome of the … -
queryset based on related model
I've got two models. One (FAQ) is related to the other (FAQCategory). I'm trying to develop a queryset of FAQCategories that are assigned to FAQs, but only if at least one of the FAQs has both the question and answer filled in. class FAQCategory(models.Model): title = models.CharField(max_length=50, null=True, blank=True) def __str__(self): return self.title class FAQ(models.Model): category = models.ForeignKey(FAQCategory, on_delete=models.SET_NULL, null=True, blank=True) question = models.CharField(max_length=100, null=True, blank=True) answer = models.TextField(max_length=10000, null=True, blank=True) For example, we have 3 FAQCategories 1) General 2) Shipping 3) Warrenty 1) General is not assigned to any faqs. It should not show up in the queryset 2) Shipping is assigned to a FAQ instance, but that FAQ doesn't have an answer on it. Shipping should not show up in the queryset 3) Warrenty is assigned to a FAQ. That FAQ has both an answer and a question value. As a result, Warrenty should appear in the queryset. How would I write this in django? Thanks! -
how to render common things in footer.html in django
i am new here in django, i have setup the layout, which contains index.html, header.html and footer.html, in footer.html i want to set dynamically phone number, email and address, i can see i can pass it through context from views.py, but footer.html will include in all the templates, so if i will write code in views.py in every function i need to write it, which is not good, so what i want, i want to create one common function and i want to call it from footer.html, is that the right way to do so ? or if you have any other idea then please let me know how to do that ? here i have added my footer.html views.py def index(request): portal_contact_email = preferences.MyPreferences.portal_contact_email context = {'portal_contact_email': portal_contact_email,} return render(request, 'mysite/index.html', context) footer.html <footer class="bg-dark footer-section"> <div class="section"> <div class="container"> <div class="row"> <div class="col-md-4"> <h5 class="text-light">Email</h5> <p class="text-white paragraph-lg font-secondary">{{ portal_contact_email }} </p> </div> <div class="col-md-4"> <h5 class="text-light">Phone</h5> <p class="text-white paragraph-lg font-secondary"></p> </div> <div class="col-md-4"> <h5 class="text-light">Address</h5> <p class="text-white paragraph-lg font-secondary"></p> </div> </div> </div> </div> <div class="border-top text-center border-dark py-5"> <p class="mb-0 text-light">Copyright @<script> var CurrentYear = new Date().getFullYear() document.write(CurrentYear) </script> {# a theme by <a href="https://themefisher.com">themefisher.com</a>#} </p> </div> … -
How to extend signup model of django-allauth and add firstname and lastname fields?
I am using django-allauth app and to extend user model I added firstname and lastname in models.py as below: class CustomUser(AbstractUser): first_name = models.CharField(max_length=128) last_name = models.CharField(max_length=128) then added its associated form in forms.py within accounts app: class CustomUserCreationForm(UserCreationForm): class Meta: model = get_user_model() fields = ('email', 'first_name', 'last_name', 'username') finally customized the urls.py file within accounts app and added a view to use customized above stated form and model: urlpatterns = [ path('signup/', SignupView.as_view(), name='sign_up'), ] and in views: class SignupView(CreateView): model = CustomUser form_class = CustomUserCreationForm template_name = 'account/signup.html' I passed the form in template and it is not showing firstname and lastname fields. -
Django Rest Framework and React Front End: How to serve sensitive images only to the user that has the correct Token authentication?
My understanding is that on a normal React website all the media will be served from the same server than the front-end files (HTML, CSS and Javascript). The REST API backend will only provide links to where those images are stored, and it will be served and displayed by the front end. The problem is if those images are private and only one user should be able to see them. If someone gets ahold of the image link and opens it they would be able to see the image. How do modern websites get around this problem? What would be the workflow between Django and React to keep the user's images safe and private? Can the private images be stored in the REST API server and only be sent as a response to a user if it has the proper token? Many thanks in advance! -
How to get a specific foreign key value
My models.py look like that: class Order(models.Model): items = models.ManyToManyField(OrderItem) class OrderItem(models.Model): item = models.ForeignKey(Item, on_delete=models.CASCADE) class Item(models.Model): quantity = models.IntegerField(default=1) In my function I have access to the Order table and I am trying to get information about the quantity of Item. I tried that: order.items.item.quantity, but I get that error: 'ManyRelatedManager' object has no attribute 'item' How would I get the quantity number from Item table?