Django中无法访问主键:孵化器详情页跳转报错求助
Hey there! I totally get how frustrating it is when you’re sure you’re on the right track (using primary keys makes total sense here) but keep hitting errors. Even with your URLs sorted, there are a handful of common pitfalls in the models and views that could be tripping you up. Let’s break down the most likely culprits:
Model-level misconfigurations
- Double-check if your Incubator model uses a custom primary key instead of Django’s default
idfield. If you’ve set a field withprimary_key=True(likeincubator_uuid), you need to make sure your view is referencing that exact field instead of assumingpkmaps toid. - Ensure all fields you’re trying to display in the detail view are actually defined in your Incubator model—typos like
incubator_namevsnameare super easy to miss.
- Double-check if your Incubator model uses a custom primary key instead of Django’s default
View logic mistakes
- Did you use
get_object_or_404correctly? It should look something likeincubator = get_object_or_404(Incubator, pk=pk)—if you swapped the model class or used the wrong parameter name (e.g., usingincubator_idinstead ofpkwhen your URL uses<int:pk>), this will fail. - Don’t forget to pass the retrieved incubator object to your template context! If your
rendercall doesn’t include something like{'incubator': incubator}, your template won’t have access to the data and will throw a variable not found error. - If you’re using a class-based view (like
DetailView), make sure you’ve set themodelattribute correctly and that thecontext_object_namematches what you’re using in the template.
- Did you use
Template rendering issues
- Verify your link to the detail page is passing the correct primary key. It should be something like
{% url 'incubator_detail' incubator.pk %}—watch out for typos (e.g.,incubators.pkinstead ofincubator.pk) or using the wrong field (likeincubator.idwhen your model uses a custom PK). - Check that you’re referencing the right field names in your detail template. If your model has a
descriptionfield but you wrote{{ incubator.desc }}in the template, you’ll get an empty value or an attribute error.
- Verify your link to the detail page is passing the correct primary key. It should be something like
Type mismatch problems
- If your Incubator model uses a non-integer primary key (like a UUID or string), your URL path converter needs to match. For example, if you’re using UUIDs, your URL should use
<uuid:pk>instead of<int:pk>—using the wrong converter will cause Django to look for an object with a mismatched ID type, resulting in a 404.
- If your Incubator model uses a non-integer primary key (like a UUID or string), your URL path converter needs to match. For example, if you’re using UUIDs, your URL should use
These are all super common "oops" moments that even experienced devs run into. Start by checking the simplest things first (like typos in field names or parameter names) and work your way through the list—chances are you’ll spot the issue quickly!
内容的提问来源于stack exchange,提问作者Arjun Kashyap

