I’m sorry, but I don’t know what additional information that I can provide that hasn’t already been given to you in this thread and your other threads concerning urls. Your last three posts are asking questions that we have already answered.
Ken thank you for your help. I will keep posting notes to myself as I try to resolve this.
If you inherit from an UpdateView, then it expects to take the pk of the specified model in the URL. This is used to fetch the model and when a POST request is made to the URL it updates the object from the form data in the body of the request.
I think you possibly want a ListView for ingredients (which is what you want in your top nav) before the update view itself for each ingredient which would be generated on the list view of ingredients
Generally a single URL (which has a unique name) calls a single view.
You seem to be confused by what the Generic Class Based views provide, so I would recommend that you go back to function based views until you better understand how a functioning django site works
am suggesting multiple functions. functions are going to be clearer for you because you get to see everything that is happening.
something like this:
in your urls.py:
- in your views.py:
def ingredients_list(request):
context = {
"ingredients_list": Ingredient.objects.all()
}
return render("ingredients/list.html", context)
def ingredients_update(request, pk):
ingredient = Ingredient.objects.get(pk=pk)
if request.method == "POST":
form = IngredientForm(data=request.POST, instance=ingredient)
if form.is_valid():
form.save()
return redirect("ingredient_list")
else:
form = IngredientForm()
return render("ingredients/update.html", {"form":form, "ingredient": ingredient})
then the 2 templates - list.html
{% for ingredient in ingredients_list %}
<p><a href="{% url 'ingredients_update' ingredient.pk %}">Update {{ ingredient.name }} </p>
{% endfor %}
update.html has a the form to update a singular ingredient
- [2:28 PM]
Treat this as pseudo code as I have just typed this from sratch without testing it.
- [2:30 PM]
For a simple site, each model you have is going to likely have at a minimum 4 urls and 4 views. One to create a new object, one to view a singular object, one to update the object, one to list multiple objects
yes. there are 2 urls and 2 views with 2 templates. The only time they are linked to each other is when we redirect back to the list view after an update. And in the list view we link to the update view providing each pk as we iterate through each ingredient.
In particular the template of the list view shows how to resolve the error you are getting now and why I think you want to have a list view in your top nav, not an update view directly
I was rereading the comments that you mentioned and you did say to go to classy CBV and look at Updateview and than look at the def get_success_url(self) method.
I visited the classy class based views website and found UpdateView and found the get_success_url method. I copied in the one of the two ways that it had it written it out and imported from django.core.exceptios the improperly configured class. After doing that I think I did what you were talking suggesting!
views.py
def get_success_url(self):
"""Return the URL to redirect to after processing a valid form."""
if not self.success_url:
raise ImproperlyConfigured("No URL to redirect to. Provide a success_url.")
return str(self.success_url) # success_url may be lazy
Did I do it correctly?
Now I have this error
Exception Type: NoReverseMatch at /
Exception Value: Reverse for 'ingredientupdate' with no arguments not found. 1 pattern(s) tried: ['ingredient/update/(?P<pk>[^/]+)\\Z']
urls.py
urlpatterns = [
path('', views.home, name='default'), # users don't need to see the rocket page anyway. they need to see the home page.
path('admin/', admin.site.urls),
path('finance/', views.finance, name='finance'),
path('home/', views.home, name='home'), #I am attempting to connect the home_view function with the views function.
path('ingredients/', views.IngredientsView.as_view(), name='ingredients'),
path('menu/', views.MenuView.as_view(), name='menu'),
path('purchases/', views.PurchaseView.as_view(), name='purchases'),
path('menu/add/',views.MenuAdditionView.as_view(success_url = "/menu/"), name="menuadd"),
path('ingredients/add/', views.IngredientAdditionView.as_view(success_url = "/ingredients/"), name='ingredientadd'), # if class based view it requires an as_view
path('ingredient/update/<pk>', views.UpdateIngredientView.as_view(success_url = "/ingredients/"), name='ingredientupdate'),
path('recipe/add/', views.RecipeRequirementAdditionView.as_view(success_url = "/menu/"), name='recipeadd'),
path('purchases/add/', views.PurchaseAdditionView.as_view(success_url = "/purchases/"), name = 'purchaseadd'),
path('update/inventory/<int:pk>/', views.IngredientsListUpdateView.as_view(), name='updateinventory'), # update view so the view had to be edited.
# cannot have conflicting path names or matching names
# finance is not a classed based view therefore i do not need an as_view
# error message views.finance() type error means I a. calling the fucntion wrong or I am not supposed to be calling it.
# It needs to know what it is updating.
]
It seems I have input a value for the ‘ingredientupdate’ url so I have that going for me.
- A
to_url(self, value)method, which handles converting the Python type into a string to be used in the URL. It should raiseValueErrorif it can’t convert the given value. AValueErroris interpreted as no match and as a consequence [reverse()
Based on what I have been told it is wrong for me to have a get_success_url with a reverse() due to python issues issues with class attributes having subject to change values. Instead I have overwritten the get success url method…well I pasted in what the Classy Based Views website had. So do I need to alter it somehow? Do I need to implement a reverse_lazy and specify the primary key of the model that I am changing inside that function?
I am sorry for how difficult this has been. It has cost me a lot of time and effort but I am trying to get better.
It seems to me that whenever I use a classed based view I need to review that website and understand the methods underneath it.
url and slug
Hello Here !
I’m also a beginner but according to your app urls, you need to try this
<p><a href="{% url 'ingredientupdate' ingredient.pk %}">Update {{ ingredient.name }} </p>