django-views

Django ForeignKey("x") field must be a "x" instance on update

Django ForeignKey("x") field must be a "x" instance on update Question: Django version 4.2.9 I try to fix this : Cannot assign "(‘customer’,)": "Project.customer" must be a "Customer" instance. I have 2 classes, Project and Customer. class Customer(models.Model): customer_name = models.CharField(max_length=50) string = models.CharField(max_length=50) def __str__(self): return f"{self.customer_name}" class Project(models.Model): project_name = models.TextField() project_code = …

Total answers: 1

I am trying to import views.py to urls.py using from . import views but I keep getting an import error

I am trying to import views.py to urls.py using from . import views but I keep getting an import error Question: from django.contrib import admin from django.urls import path from . import views urlpatterns = [ path(‘admin/’, admin.site.urls), path("", views.home), path("predict/", views.predict), path("predict/result", views.result) the traceback is File "C:UsersuserPycharmProjectsDiabetes PredictionDiabetes_PredictionDiabetes_Predictionurls.py", line 19, in from . …

Total answers: 2

Is it possible to make Django urlpatterns from a string?

Is it possible to make Django urlpatterns from a string? Question: I have a list of strings that is used to define navigation pane in a Django layout template. I want to use the same list for view function names and use a loop to define urlpatterns in urls.py accordingly. Example: menu = ["register", "login", …

Total answers: 1

Add Radio Button in gender feied in crud django

Add Radio Button in gender feied in crud django Question: i have a CRUD PROJECT in which i need gender field in radio buttons or in check box i tried to apply those but did not getr the desired result, Radio buttons are not adding if they are adding they are not posting data in …

Total answers: 2

How to filter an item in one queryset from appearing in another queryset in django

How to filter an item in one queryset from appearing in another queryset in django Question: I have a Django view where I am getting the first queryset of ‘top_posts’ by just fetching all the Posts from database and cutting it in the first four. Within the same view, I want to get "politics_posts" by …

Total answers: 1

Django HttpResponseRedirect() doesn't work – it doesn't redirect to the page

Django HttpResponseRedirect() doesn't work – it doesn't redirect to the page Question: I was trying to implement the function listing(request, listId) that when user wants to post a comment without login, it will be redirected to the login page. This is the code of my listing(request, listId) in views.py def listing(request, listId): if request.method == …

Total answers: 1

foregin key using name value fetch in django rest frame work

foregin key using name value fetch in django rest frame work Question: i have add model.py ,view.py and serilializer.py file ,i will share country and state model . I wiil apply foregin key country and state fields. I will check in html fields in django rest framework in only object id fetch not value so …

Total answers: 2

Select 3 random items and exclude the one already selected

Select 3 random items and exclude the one already selected Question: I’m trying to display three random drinks in my template, selected from a database of drinks. I currently have the random selection working but also noticed theres a chance of a drink showing up twice. How do I prevent this from happening? Is there …

Total answers: 2

Strange behaviour because of the order of URLs in urls.py django behaviour

Strange behaviour because of the order of URLs in urls.py django behaviour Question: I am doing a mini blog assignment using Django for learning purpose. I noticed a strange behavior in django urls.py when the order of lines is below I am able to make a successful GET call to http://127.0.0.1:8000/blog/blog/create path(‘blog/create’, views.BlogPostCreate.as_view(), name=’blogpost-create’), path(‘blog/<slug:slug>’, …

Total answers: 1