URLconf for foreign key in a model
Not sure how to title this but I was trying to look up how to put a foreign key value into the URL address.
Here is some example code of what I am trying to do:
#models.py
class Client(models.Model):
name = models.CharField(max_length = 13)
class Product(models.Model):
client = models.ForeignKey(Client, on_delete=models.CASCADE)
name = models.CharField(max_length = 80)
description = models.TextField(blank=True, null=True)
# views.py
class ClientView(generic.DetailView):
model = Client
template_name = 'app/client.html'
class ProductView(generic.DetailView):
model = Product
template_name = 'app/product.html'
#url.py
urlpatterns = [
url(r'^profile/(?P<pk>[0-9]+)/$', views.ClientView.as_view(), name='client'),
## This I am trying to preserve the foreign key and use it here for client
url(r'^profile/(?P<client__pk>[0-9]+)/(?P<pk>[0-9]+)/$', views.ProductView.as_view(), name='product'),
]
Basically I am trying to add the primary key from client into the URL for the product page. Is there a good way to do this?
/r/djangolearning
https://redd.it/75ncw5
Not sure how to title this but I was trying to look up how to put a foreign key value into the URL address.
Here is some example code of what I am trying to do:
#models.py
class Client(models.Model):
name = models.CharField(max_length = 13)
class Product(models.Model):
client = models.ForeignKey(Client, on_delete=models.CASCADE)
name = models.CharField(max_length = 80)
description = models.TextField(blank=True, null=True)
# views.py
class ClientView(generic.DetailView):
model = Client
template_name = 'app/client.html'
class ProductView(generic.DetailView):
model = Product
template_name = 'app/product.html'
#url.py
urlpatterns = [
url(r'^profile/(?P<pk>[0-9]+)/$', views.ClientView.as_view(), name='client'),
## This I am trying to preserve the foreign key and use it here for client
url(r'^profile/(?P<client__pk>[0-9]+)/(?P<pk>[0-9]+)/$', views.ProductView.as_view(), name='product'),
]
Basically I am trying to add the primary key from client into the URL for the product page. Is there a good way to do this?
/r/djangolearning
https://redd.it/75ncw5
reddit
URLconf for foreign key in a model • r/djangolearning
Not sure how to title this but I was trying to look up how to put a foreign key value into the URL address. Here is some example code of what I...
I made my large flask project open source! CryptoCurrency based community with wallets.
This has been a nonstop project during covid. It was on gitea but moved it to github. Its made with Python flask/ flasksqlalchemy / postgres. I decided to work on the project with my wife after she got a reddit gold and realized it was worth nothing and complained it should be a bitcoin or crypto something. Then we found out it probably benefits tencent (China) the most. So work began to bring the coin back to the people.
#Github
https://github.com/tipvote
#url
www.tipvote.com
#contribute or advice
Looking for people who want to contribute or help out. Also offering advice if you need help with a crypto based service.
/r/flask
https://redd.it/iv9xcq
This has been a nonstop project during covid. It was on gitea but moved it to github. Its made with Python flask/ flasksqlalchemy / postgres. I decided to work on the project with my wife after she got a reddit gold and realized it was worth nothing and complained it should be a bitcoin or crypto something. Then we found out it probably benefits tencent (China) the most. So work began to bring the coin back to the people.
#Github
https://github.com/tipvote
#url
www.tipvote.com
#contribute or advice
Looking for people who want to contribute or help out. Also offering advice if you need help with a crypto based service.
/r/flask
https://redd.it/iv9xcq
GitHub
Tipvote
A Social Media Website. Tipvote has 9 repositories available. Follow their code on GitHub.
Problem creating a basic hello world app
I'm a complete beginner with Django and I'm trying to create a basic hello world app but I have been getting this error:
Request Method:GET
Request URL:http://127.0.0.1:8000/playground/hello/
Using the URLconf defined in Django_project.urls, Django tried these URL patterns, in this order:
admin/
The current path, playground/hello/, didn’t match any of these.
​
I created an app with the _python manage.py startapp_ command, called playground.
​
Code in playground.views.py
​
from django.shortcuts import render
from django.http import HttpResponse
def say_hello(request):
return HttpResponse('Hello World')
​
Code in playground.urls.py
from django.urls import path
from .views import views
​
#url configuration
urlpatterns = [
path('hello/', views.say_hello)
\]
​
and of course in django.urls.py
i added the include function to urlpatterns
​
from django.contrib import admin
from django.urls import path, include
​
​
urlpatterns = [
path('admin/', admin.site.urls),
path('playground/', include('playground.urls'))
\]
So where exactly did i go wrong?
/r/djangolearning
https://redd.it/pf7ig0
I'm a complete beginner with Django and I'm trying to create a basic hello world app but I have been getting this error:
Request Method:GET
Request URL:http://127.0.0.1:8000/playground/hello/
Using the URLconf defined in Django_project.urls, Django tried these URL patterns, in this order:
admin/
The current path, playground/hello/, didn’t match any of these.
​
I created an app with the _python manage.py startapp_ command, called playground.
​
Code in playground.views.py
​
from django.shortcuts import render
from django.http import HttpResponse
def say_hello(request):
return HttpResponse('Hello World')
​
Code in playground.urls.py
from django.urls import path
from .views import views
​
#url configuration
urlpatterns = [
path('hello/', views.say_hello)
\]
​
and of course in django.urls.py
i added the include function to urlpatterns
​
from django.contrib import admin
from django.urls import path, include
​
​
urlpatterns = [
path('admin/', admin.site.urls),
path('playground/', include('playground.urls'))
\]
So where exactly did i go wrong?
/r/djangolearning
https://redd.it/pf7ig0