基于Django的端口扫描网站:单选框触发对应视图函数调用问题
Let's work through this step by step to get your port scan website functioning as intended. First, we'll fix the core issues in your original code, then refine your jQuery approach (plus offer a pure Django alternative if you prefer avoiding JS).
1. Fix Your URL Configuration
Your current urls.py uses empty strings for all patterns, which means Django will always match the first view (views.home) and never reach your scan functions. Update it to use distinct paths:
from django.urls import path # Use modern path instead of the old url function from . import views urlpatterns = [ path('', views.home, name='home'), path('fullscan/', views.fullscan, name='fullscan'), path('quickscan/', views.quickscan, name='quickscan'), ]
2. Correct Your View Functions
Django view functions require the request object as their first parameter. Let's adjust them properly:
from django.shortcuts import render def home(request): return render(request, 'base.html') # Render your home template here def fullscan(request): # Add your full port scan logic here return render(request, 'scan_results.html', {'scan_type': 'Full Scan'}) def quickscan(request): # Add your quick port scan logic here return render(request, 'scan_results.html', {'scan_type': 'Quick Scan'})
3. Refine the jQuery Approach
Your initial JS idea is solid—let's polish it to ensure reliability and clarity:
<div id="search-section"> <form id="suggestion_form" name="gs"> <div class="searchText"> <ul> <li> <label> <input type="radio" name="ext_com" data-location="{% url 'fullscan' %}" autocomplete="off" checked> Full Scan </label> </li> <li> <label> <input type="radio" name="ext_com" data-location="{% url 'quickscan' %}" autocomplete="off"> Quick Scan </label> </li> </ul> </div> <input type="button" id="startScanBtn" name="results" class="main-button" value=" Start Now "> </form> </div> <script src="https://code.jquery.com/jquery-1.8.0.min.js"></script> <script> $(document).ready(function() { $("#startScanBtn").on('click', function() { // Grab the URL from the selected radio button's data attribute const targetUrl = $('input[name="ext_com"]:checked').data('location'); if (targetUrl) { window.location.href = targetUrl; } return false; }); }); </script>
Key Improvements:
- Wrapped the JS in
$(document).ready()to ensure the DOM is fully loaded before attaching the click event - Used a more precise selector (
$('input[name="ext_com"]:checked')) to target the selected radio button - Added a safety check for
targetUrlto avoid errors (though we have a default checked option) - Removed unnecessary form action since we're handling navigation via JS
Alternative: Pure Django Approach (No JavaScript)
If you want a more "Django-native" solution without relying on JS, use a router view to handle form submission:
Template:
<form id="suggestion_form" name="gs" method="post" action="{% url 'scan_router' %}"> {% csrf_token %} <!-- Required for all Django POST forms --> <div class="searchText"> <ul> <li><label><input type="radio" name="ext_com" value="full" checked>Full Scan</label></li> <li><label><input type="radio" name="ext_com" value="quick">Quick Scan</label></li> </ul> </div> <input type="submit" name="results" class="main-button" value=" Start Now "> </form>
Add Router View to views.py:
from django.shortcuts import redirect def scan_router(request): if request.method == 'POST': scan_type = request.POST.get('ext_com') if scan_type == 'full': return fullscan(request) elif scan_type == 'quick': return quickscan(request) return redirect('home')
Update urls.py with the router path:
urlpatterns = [ path('', views.home, name='home'), path('scan-router/', views.scan_router, name='scan_router'), path('fullscan/', views.fullscan, name='fullscan'), path('quickscan/', views.quickscan, name='quickscan'), ]
This approach is great for accessibility and avoids potential JS-related issues.
内容的提问来源于stack exchange,提问作者Yash Mistry

